Weird issues with network.request get json data

Hi,

I have a sceen in my app where the tableview data is json decoded from a wordpress url. Now my issue is that the data updates behave really have really weird. For most of the time its not getting uppdated even if the app is restarted. Thats both on iOS device and in the simulator. This just started to happen couple of days ago. Before that a simple restart of the app would update the tableview. First I thought this is because Im not cleaning up the scene as I have been lazy doing that and started with that first, but the problem presists. Then I thought its because I’m saving the son data to a file and loading it so deleted the file when exiting the scene but that didn’t fix the issue either. After that I gave up and got the json data from a php file insted and now the tableview always has the latest data when existing and going in to the scene again. The only issue I have with that is that its a little bit slower and most of the error handling has to be done in the php file. Have anybody had this issue before? The only thing that I have changed with the code that fixed the issue is the url that is pointing to the php file now. What I did notice is that the json data that is being returned has some invalid characters that are html tags in it according to http://jsonformatter.curiousconcept.com/ but its still working as it should, just wasn’t getting the latest data.

I get JSON data from the cloud in my app and it works fine, including in the latest daily build. Your hunch about invalid characters seems like you’re on the right track to me. If your JSON data has multi-octet \xxx sequences you may need to manually encode them to UTF-8. I recall people have posted code excerpts before that might help.

Hi @sam.bavi,

This tutorial may help you out:

http://coronalabs.com/blog/2014/11/11/tutorial-encoding-urls-for-network-usage/

Brent

Wordpress and PHP cache stuff add “randid=” … tostring(math.random(10000)) to your query string and see if that helps.

This is what Im using at the moment that works. The url inside the php is what I was using inside the app before. Everything is showing as It should. The json is from wp json api, as you can se Im not decoding or encoding the data or the url. Ive checked the link that Brent gave me, but Im not sure If its any help here? I will try that Bishop and get back and se what happens =)) The thing is that before using php the data did eventually get fetched and updated just that it could take anywhere between 15min to 1-2 hours. And this just happens when doing It through the app. Using postman or php always has the new data updated.

<?php

//echo “Test url”;

$url = "http://www.teamhokuspokus.com/testsite/wp-json/posts";

$json = file_get_contents($url);

//$data = preg_replace(’/[]{2,}|[\t]/’, ’ ', trim($json));

//$json_data =  json_encode($json, true);

echo $json;

?>

What does your lua look like? after it does the network request that is.

btw the url in your example above doesn’t even return json for me but rather the complete blog history, browser or pure sockets produce the same result.

Try the url now, it was pointing to the wrong adress =)) It looked like everything was working, the actual network request looked like It did take place and it printed out all the requested json tables through a for loop after being decoded inside the network request, but  the lastest posts were always missing for the amount of time that I described before. At the same time I could se the posts showing up through the same url on postman and through php, and thats what made It weird for me. What I should add is that the son file thats coming back through that link is valid, but the json data coming from my php file is not. Still the php data is working exactly the same way as doing it inside the app.

I think the problem is on the server side not the corona side.

Even just stepping outside of corona and running

curl "http://www.teamhokuspokus.com/testsite/wp-json/postsn"

Will result in a “No route was found matching the URL and request method” message which is a WP issue.

Thanks for the help everybody =)) If i get a fix on it I will update this post…

I get JSON data from the cloud in my app and it works fine, including in the latest daily build. Your hunch about invalid characters seems like you’re on the right track to me. If your JSON data has multi-octet \xxx sequences you may need to manually encode them to UTF-8. I recall people have posted code excerpts before that might help.

Hi @sam.bavi,

This tutorial may help you out:

http://coronalabs.com/blog/2014/11/11/tutorial-encoding-urls-for-network-usage/

Brent

Wordpress and PHP cache stuff add “randid=” … tostring(math.random(10000)) to your query string and see if that helps.

This is what Im using at the moment that works. The url inside the php is what I was using inside the app before. Everything is showing as It should. The json is from wp json api, as you can se Im not decoding or encoding the data or the url. Ive checked the link that Brent gave me, but Im not sure If its any help here? I will try that Bishop and get back and se what happens =)) The thing is that before using php the data did eventually get fetched and updated just that it could take anywhere between 15min to 1-2 hours. And this just happens when doing It through the app. Using postman or php always has the new data updated.

<?php

//echo “Test url”;

$url = "http://www.teamhokuspokus.com/testsite/wp-json/posts";

$json = file_get_contents($url);

//$data = preg_replace(’/[]{2,}|[\t]/’, ’ ', trim($json));

//$json_data =  json_encode($json, true);

echo $json;

?>

What does your lua look like? after it does the network request that is.

btw the url in your example above doesn’t even return json for me but rather the complete blog history, browser or pure sockets produce the same result.

Try the url now, it was pointing to the wrong adress =)) It looked like everything was working, the actual network request looked like It did take place and it printed out all the requested json tables through a for loop after being decoded inside the network request, but  the lastest posts were always missing for the amount of time that I described before. At the same time I could se the posts showing up through the same url on postman and through php, and thats what made It weird for me. What I should add is that the son file thats coming back through that link is valid, but the json data coming from my php file is not. Still the php data is working exactly the same way as doing it inside the app.

I think the problem is on the server side not the corona side.

Even just stepping outside of corona and running

curl "http://www.teamhokuspokus.com/testsite/wp-json/postsn"

Will result in a “No route was found matching the URL and request method” message which is a WP issue.

Thanks for the help everybody =)) If i get a fix on it I will update this post…