php - 在 android 中获取 HTML 响应而不是 JSON

标签 php android mysql json

我正在尝试通过 php 将位于远程服务器上的 mysql 数据库中的数据检索到 android 中。这在本地主机上运行时运行良好。但是当它在远程服务器上时,我从服务器收到 HTML 代码而不是 JSON 响应。我尝试在浏览器中粘贴 url ( http://ksos.0fees.us/pgm_list.php?day=1&hall=A),这导致了正确的 JSON 输出。 HTML 响应如下所示。

<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>

以下是我向服务器请求响应

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+"\n");
        }
        is.close();
        response = sb.toString();
    }catch(Exception e){
        Log.e("Buffer Error", "Error : "+e.toString());
    }
    return response;
}

我尝试过为用户代理设置和不设置 setHeader。 php 部分如下所示:

$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);
}

最佳答案

Atlast 找到解决方案...

问题不在于 android 或 php 部分。这是服务器的问题。我使用的托管站点将 cookie 发送到客户端,这不是在 android 内部处理的,而是由浏览器自动处理的。我曾经使用过另一个不涉及 cookie 的托管站点,并获得了所需的 json 输出。

关于php - 在 android 中获取 HTML 响应而不是 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32409655/

相关文章:

php - PHP(如果单或双等于)

android - 以编程方式将 Textview 选取框

php - 使用 ZEROFILL 精确比较两个数字

android - WebRtcAudioRecord.context 由泄漏金丝雀报告泄漏。怎么解决?

Android 抽屉导航最大项目数

php - 从特定的 MySql 行获取值

mysql - 结合三个选择查询?

javascript - 关闭弹出窗口后如何重新加载现有页面

php - 如何测试作为参数传递给函数的 PHP Closure 是否被调用了准确次数

php - 如何设置 html 文件以使用热敏打印机进行打印