【发布时间】:2015-12-01 07:07:28
【问题描述】:
我正在尝试通过 php 从位于远程服务器上的 mysql 数据库中检索数据到 android。这在 localhost 中运行时效果很好。但是当它在远程服务器中时,我会收到 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);
}
【问题讨论】:
-
请在您将输出回显到您的 android 应用程序的地方发布 php 代码
-
我不希望 php 出现任何问题,因为当我在任何浏览器中检查 URL 时,我得到了所需的输出
-
问:这其中哪一部分不是服务器端的问题?问:您是否编写(或控制)服务器端 PHP 应用程序?问:您的 localhost 配置是否使用 same PHP 应用程序和 same HTTP 服务器?问:服务器日志说什么? “本地主机”和“远程”HTTP 请求之间有什么区别吗?您从服务器端完成了多少故障排除?
-
上面给出的php只是服务器端的。本地主机使用与远程服务器完全相同的东西并给出了正确的结果。我还没有完成服务器端故障排除。我到底应该在那里做什么?
-
您的问题几乎肯定出在服务器端。一台服务器发回纯 JSON(我猜,根据你的说法),另一台发回 HTML + Javascript。您需要弄清楚为什么服务器响应不同。再次: 问:您是在本地运行 same PHP 应用程序,因为远程服务器正在运行......还是在本地“模拟”它?问:本地和远程是否具有相同类型的 HTTP 服务器/相同的 PHP 配置?问:您是否查看过服务器日志(本地和远程)?您是否在日志中看到相同的“请求”?问:身份验证会是个问题吗?