0

以下代码未从 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;
  }
}

非常感谢!

4

3 回答 3

1

您的网址http://ntusocities.host22.com/post.php返回badminton society字符串。这不是一个有效的 JSON,因此 JSON 解析失败也就不足为奇了。您需要修复服务器端以返回有效的 JSON。

什么是 JSON?-> http://en.wikipedia.org/wiki/JSON

如何检查 JSON 是否有效?-> http://jsonlint.com/

此外,还有一种非常简单的方法可以将响应作为字符串获取:https ://stackoverflow.com/a/4480517/247013

于 2013-04-10T18:59:36.977 回答
0

你在 AndroidManifest.xml 中添加了权限吗

<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

您提到的链接,提供数据

{"info":[{"SocietyID":"SOC003","Name":"Badminton","Type":"Sport","President":"Me","VicePresident":"You","ContactEmail":"me@gmail.com","Description":"badminton society"}]}<!-- some data-->

Just remove, '[' and ']' and "<!-- some data-->" from your response. I think the "some data part    is comment.

其他一切都很好。

然后尝试此代码以获取值,

JSONObject userObject;
try {
userObject = new JSONObject(content);
JSONObject info = userObject.getJSONObject("info");
String name = info.getString("Name");
String type = info.getString("Type");
String description = info.getString("Description");
    // like this fetch all value/fields

Log.i("JOSN", name +", "+ type +", "+ description);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

一切顺利 !

于 2013-04-10T17:14:42.143 回答
0

好吧,url的输出是:

"badminton society"
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->

意味着它不是 JSON,就像 Arhimend 已经提到的那样。但这不应阻止您获得有效的 InputStream。尝试这个:

protected Void doInBackground(Void... params) {
//http post
try{
  is = new java.net.URL("http://ntusocities.host22.com/post.php").openStream();
} catch(Exception e){
  Log.e("log_tag", "Error in http connection ", e);
}
...

无论如何,代码将无法解析

 JSONObject userObject = new JSONObject(result);

因为“结果”不是 JSON。

于 2013-04-10T19:26:42.833 回答