gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 07:25:22 24 4
gpt4 key购买 nike

我正在尝试通过 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com