我试图通过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);
}
答案 0 :(得分:4)
Atlast找到了解决方案......
问题不在于android或php部分。这是服务器的问题。我使用的托管网站向客户端发送cookie,这些cookie在android内部没有处理,但是由浏览器自动处理。我习惯了另一个不涉及cookie的托管网站,并获得了所需的json输出。
答案 1 :(得分:1)
我花了很长时间来理解并解决问题。
首先,我们需要了解0fess托管具有反僵尸技术,可阻止来自(无浏览器)客户端的呼叫。这种技术的主要思想是使用一个javascript脚本来检查请求是否来自普通的Web浏览器,然后脚本加密IP并设置一个带有密钥__test和加密IP值的cookie。
要解决此类问题,我们需要在我们的应用程序中运行webview并从我们的0fees站点请求任何页面。然后我们可以拦截响应并获取cookie。之后,我们可以在我们的http休息请求中使用此cookie作为请求标头。
这是一个示例代码
WebView browser=new WebView(getContext());
browser.getSettings().setJavaScriptEnabled(true);
browser.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url){
final String cookies = CookieManager.getInstance().getCookie(url);
Log.d("any", "All the cookies by me in a string:" + cookies);
HttpPost httppost = new HttpPost("http://ksos.0fees.us/pgm_list.php");
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Cookie", cookies);
//continue your request paramters
}
}
);
browser.loadUrl("http://ksos.0fees.us/");