Home > Article > Backend Development > Why Does My Android App Receive HTML Instead of JSON When Accessing a ByetHost Server?
ByetHost Server Passing HTML Values 'Checking Your Browser' with JSON String: Android App Issue
Problem:
While parsing JSON strings in an Android app, HTML values are being passed. This issue occurs when accessing a ByetHost server with PHP files, but not other servers.
Solution:
ByetHost employs a security module called testcookie-nginx-module that adds an extra layer of validation to HTTP requests. This module follows a two-step process:
Android App Implementation:
To resolve the issue in the Android app, follow these steps:
<code class="java">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 ..."); httpPost.addHeader("Cookie", "__test=" + cookieContent + "; expires=" + cookieExpires + "; path=" + cookiePath); 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=" + cookieContent + "; expires=" + cookieExpires + "; path=" + cookiePath); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } }</code>
Note: Replace cookieContent, cookieExpires, and cookiePath with the values you obtained from the browser.
This modification will ensure that your Android app sends the validation cookie with every HTTP request, bypassing the initial HTML redirect and allowing it to retrieve the JSON data.
The above is the detailed content of Why Does My Android App Receive HTML Instead of JSON When Accessing a ByetHost Server?. For more information, please follow other related articles on the PHP Chinese website!