在Android中获取HTML响应,而不是JSON [英] Getting HTML response instead of JSON in android

查看:279
本文介绍了在Android中获取HTML响应,而不是JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从位于远程服务器上,通过PHP到Android的mysql数据库的数据。这工作正常在本地主机运行时。但是,当它在远程服务器上,我收到,而不是从服务器的JSON响应HTML codeS。我试图粘贴URL( http://ksos.0fees.us/pgm_list .PHP天= 1&安培;大厅= A )的浏览器,导致正确的JSON输出。以下HTML响应示

 < HTML><身体GT;
<脚本类型=文/ JavaScript的SRC =/ aes.js>< / SCRIPT>
<脚本>功能toNumbers(四)
 {VAR E = [];
  d.replace(/(..)/克,功能(D){e.push(parseInt函数(D,16))});
  返回电子邮件
 }
 功能toHex()
  {为(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);
  返回e.toLowerCase()
 }
 变种a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("5cb1c0309e553acda177d912f21ac485");
 的document.cookie =__测试=+ toHex(slowAES.decrypt(C,2,A,B))+;
 到期=周四,31日 -  12月37 23点55分55秒格林尼治标准​​时间;
 路径= /;
 location.href =htt​​p://ksos.0fees.us/pgm_list.php?day=1&hall=A&ckattempt=1;
&LT; / SCRIPT&GT;
&LT; NOSCRIPT&gt;这网站需要JavaScript的工作,请启用浏览器的Javascript或使用的JavaScript支持℃的浏览器; / NOSCRIPT&GT;
&LT; /身体GT;&LT; / HTML&GT;
 

以下是我的请求,服务器获得响应

 公共字符串makeServiceCall(字符串URL,诠释方法,清单和LT;的NameValuePair&GT; PARAMS){
    尝试{
        DefaultHttpClient的HttpClient =新DefaultHttpClient();
        HttpEntity httpentity = NULL;
        HTT presponse HTT presponse = NULL;

        如果(方法== POST){
            HttpPost httppost =新HttpPost(URL);
            httppost.setHeader(用户代理,UA);
            httppost.setHeader(接受,应用/ JSON);
            如果(PARAMS!= NULL){
                httppost.setEntity(新UrlEn codedFormEntity(PARAMS));
            }
            HTT presponse = httpclient.execute(httppost);
        }否则,如果(方法== GET){
            如果(PARAMS!= NULL){
                字符串中的paramString = URLEn codedUtils.format(PARAMS,UTF-8);
                网址+ =? +中的paramString;
            }
            HTTPGET HTTPGET =新HTTPGET(URL);
// HttpPost httppost =新HttpPost(URL);
            httpget.setHeader(用户代理,UA);
            httpget.setHeader(接受,应用/ JSON);
            HTT presponse = httpclient.execute(HTTPGET);
        }
        httpentity = HTT presponse.getEntity();
        是= httpentity.getContent();
    }赶上(UnsupportedEncodingException E){
        e.printStackTrace();
    }赶上(ClientProtocolException E){
         e.printStackTrace();
    }赶上(IOException异常E){
         e.printStackTrace();
    }
    尝试{
        的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(就是,UTF-8),8);
//的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(就是,ISO-8859-1),8);
        StringBuilder的SB =新的StringBuilder();
        串线= NULL;
        而((行= reader.readLine())!= NULL){
            sb.append(行+\ N);
        }
        is.close();
        响应= sb.toString();
    }赶上(例外五){
        Log.e(缓冲区错误,错误:+ e.toString());
    }
    返回响应;
}
 

我通过设置和不设置SetHeader可以为用户代理尝试。 PHP的一部分如下所示:

  $ Q =请求mysql_query(选择...........);
如果(!空($ Q)){
    而($行= mysql_fetch_assoc($ Q))
        $ pgmlist [] = $行;
    $响应[pgmlist] = $ pgmlist;
    回声json_en code($响应);

}
其他{
    $响应[成功] = 0;
    $响应[信息] =没有找到记录;
    回声json_en code($响应);
}
 

解决方案

Atlast找到解决办法......

这个问题是不是与Android或PHP的一部分。它是与服务器的问题。托管网站,我使用,发送Cookie来未里面机器人处理,但会自动被浏览器处理的客户端。我曾经在这里不涉及饼干另一个托管网站,并获得所需要的JSON输出。

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+"\n");
        }
        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);
}

解决方案

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.

这篇关于在Android中获取HTML响应,而不是JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆