下面的代码没有从PHP文件中的这段代码接收到PHP文件的输出,即“羽毛球协会”:
print(json_encode($row['Description']));
如果有人能发现这个错误那就太好了。 'result' 和 'is' 都是空变量:
public class BackgroundAsyncTask extends AsyncTask<Void, String, Void> {
protected Void doInBackground(Void... params) {
//http post
try{
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://ntusocities.host22.com/post.php");
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
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();
result=sb.toString();
} catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try {
JSONObject userObject = new JSONObject(result);
info = userObject.getString("Description");
}
catch(Exception ex){
}
return null;
}
}
非常感谢!
最佳答案
您的网址http://ntusocities.host22.com/post.php
返回羽毛球协会
字符串。这不是有效的 JSON,因此 JSON 解析失败并不奇怪。您需要修复服务器端以返回有效的 JSON。
什么是 JSON? -> http://en.wikipedia.org/wiki/JSON
如何检查 JSON 是否有效? -> http://jsonlint.com/
此外,还有一种非常简单的方法来获取字符串形式的响应:https://stackoverflow.com/a/4480517/247013
关于php - ASyncTask HTTP 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15932198/