Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

php - Getting HTML response instead of JSON in android

I'm trying to retrieve data from mysql database located on a remote server through php into android. This worked fine while running in localhost. But when it is in remote server, I receive HTML codes instead of JSON response from server. I tried pasting the url (http://ksos.0fees.us/pgm_list.php?day=1&hall=A) in browser which resulted in correct JSON output. The HTML response is shown below.

<html><body>
<script type="text/javascript" src="/aes.js" ></script>
<script>function toNumbers(d)
 {var e=[];
  d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});
  return e
 }
 function toHex()
  {for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)
  e+=(16>d[f]?"0":"")+d[f].toString(16);
  return e.toLowerCase()
 }
 var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("5cb1c0309e553acda177d912f21ac485");
 document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; 
 expires=Thu, 31-Dec-37 23:55:55 GMT; 
 path=/";
 location.href="http://ksos.0fees.us/pgm_list.php?day=1&hall=A&ckattempt=1";
</script>
<noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript>
</body></html>

Following is my request to server for getting response

public String makeServiceCall(String url, int method, List<NameValuePair> params){
    try{
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpEntity httpentity = null;
        HttpResponse httpresponse = null;

        if (method == POST) {
            HttpPost httppost = new HttpPost(url);
            httppost.setHeader("User-Agent", ua);
            httppost.setHeader("Accept", "application/json");
            if(params!=null){
                httppost.setEntity(new UrlEncodedFormEntity(params));
            }
            httpresponse = httpclient.execute(httppost);
        } else if(method == GET){
            if(params!=null){
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpget = new HttpGet(url);
//              HttpPost httppost = new HttpPost(url);
            httpget.setHeader("User-Agent", ua);
            httpget.setHeader("Accept", "application/json");
            httpresponse = httpclient.execute(httpget);
        }
        httpentity = httpresponse.getEntity();
        is = httpentity.getContent();
    }catch(UnsupportedEncodingException e){
        e.printStackTrace();
    }catch (ClientProtocolException e) {
         e.printStackTrace();
    }catch (IOException e) {
         e.printStackTrace();
    }
    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
//          BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine())!= null) {
            sb.append(line+"
");
        }
        is.close();
        response = sb.toString();
    }catch(Exception e){
        Log.e("Buffer Error", "Error : "+e.toString());
    }
    return response;
}

I've tried by setting and not setting setHeader for user agent. The php part looks as follows:

$q=mysql_query("SELECT ...........");
if(!empty($q)){
    while($row=mysql_fetch_assoc($q))
        $pgmlist[]=$row;
    $response["pgmlist"] = $pgmlist;
    echo json_encode($response);

}
else{
    $response["success"] = 0;
    $response["message"] = "No record found";
    echo json_encode($response);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Atlast found solution...

The issue was not with the android or php part. It was the problem with the server. The hosting site which I used, sends cookies to client side which is not handled inside android but is automatically handled by browsers. I used to another hosting site where cookies are not involved and got the needed json output.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...