While trying to parse json string to android, HTML values are passed. Before a day all was working good, and suddenly my app start crashing when trying to fetch database wit
Solved!
I had the same issue using Byethost to retrieve JSON data from my PHP server. We just need to add a cookie to the HTTP request to pass the testcookie-nginx-module
As Richard's answer says:
The main problem is that Byet Host implement a simple security antibots module >named testcookie-nginx-module
https://kyprizel.github.io/testcookie-nginx-module/
On the link he provided we can see that the testcookie-nginx-module makes a 2-steps validation:
Here's the script I've received form my server:
On the following HTTP requests the client will have stored the cookie and will add it to the request skipping the step 1.
Solution for our Android App
We are going to skip the cookie generation by getting it from a web browser and add it directly to our Android HTTP request (Unless of course you want to get involved in generating it).
Before you get the cookie from the web browser make sure you accessed the url at least once with the browser to generate it.
Getting the cookie key from the web browser. I used Google Chrome for it:
Setting the cookie on our Android app. On your code should be something like:
try
{
if(post == "POST")
{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(loginUrl);
httpPost.setEntity(new UrlEncodedFormEntity(para));
httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240 ");
httpPost.addHeader("Cookie", "__test=THE_CONTENT_OF_YOUR_COOKIE_HERE; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/");
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
else if(post == "GET")
{
HttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(para, "utf-8");
loginUrl += "?" + paramString;
HttpGet httpGet = new HttpGet(loginUrl);
httpGet.addHeader("Cookie", "__test=THE_CONTENT_OF_YOUR_COOKIE_HERE; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/");
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
}
And that's it. Now every time the app makes an HTTP request it will include the cookie to pass the testcookie-nginx-module and will be retrieving your JSON data.
I hope this helps and is not too late.
Regards