1

我在 android LogCat 中收到此错误消息

04-24 21:14:02.603: W/System.err(831): org.json.JSONException: Value {"id":"602414132","first_name":"Adham","username":"adham.enaya","locale":"en_GB","link":"http:\/\/www.facebook.com\/adham.enaya","name":"Adham Enaya","last_name":"Enaya","gender":"male"} of type org.json.JSONObject cannot be converted to JSONArray
04-24 21:14:02.603: W/System.err(831):  at org.json.JSON.typeMismatch(JSON.java:107)
04-24 21:14:02.603: W/System.err(831):  at org.json.JSONArray.<init>(JSONArray.java:91)
04-24 21:14:02.603: W/System.err(831):  at org.json.JSONArray.<init>(JSONArray.java:103)
04-24 21:14:02.615: W/System.err(831):  at pit.opensource.readjson.D0ReadJSONWebServiceActivity.pasreJSON(D0ReadJSONWebServiceActivity.java:85)
04-24 21:14:02.623: W/System.err(831):  at pit.opensource.readjson.D0ReadJSONWebServiceActivity.access$0(D0ReadJSONWebServiceActivity.java:78)
04-24 21:14:02.623: W/System.err(831):  at pit.opensource.readjson.D0ReadJSONWebServiceActivity$DownloadFBUser.doInBackground(D0ReadJSONWebServiceActivity.java:108)
04-24 21:14:02.623: W/System.err(831):  at pit.opensource.readjson.D0ReadJSONWebServiceActivity$DownloadFBUser.doInBackground(D0ReadJSONWebServiceActivity.java:1)
04-24 21:14:02.623: W/System.err(831):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-24 21:14:02.633: W/System.err(831):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
04-24 21:14:02.633: W/System.err(831):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-24 21:14:02.633: W/System.err(831):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-24 21:14:02.633: W/System.err(831):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-24 21:14:02.633: W/System.err(831):  at java.lang.Thread.run(Thread.java:1019)

有什么问题,我认为是字符串编码?!

更新:

代码在这里

//Download text -------------------------------------------------------------
private String DownloadText(String urlString){
    InputStream in = null;
    StringBuilder sb = new StringBuilder();
    try{
        in = OpenHttpConnection(urlString);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line="";
        while((line = br.readLine())!= null){
            sb.append(line);
        }
    }catch(IOException ex){

    }
    return sb.toString();
}

//Parse JSON text -----------------------------------------------------------
private FBUser pasreJSON(String url){
    FBUser user = null;
    JSONArray json;
    Log.d("JSON----------------",DownloadText(url));
    try {
        user = new FBUser();
        json = new JSONArray(DownloadText(url));
        for(int i=0;i<json.length();i++){
            JSONObject object = json.getJSONObject(i);
            user.id = object.getString("id");
            user.name = object.getString("name");
            user.first_name = object.getString("first_name");
            user.last_name = object.getString("last_name");
            user.link = object.getString("link");
            user.username = object.getString("username");
            user.gender = object.getString("gender");
            user.locale = object.getString("locale");
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return user;
}
4

4 回答 4

1

错误在于这不是 JSONArray,而是 JSONObject。数组用方括号括起来,而对象用大括号括起来。

看看官方网站json.org

于 2012-04-24T21:22:48.053 回答
1

在这一行:

json = new JSONArray(DownloadText(url));

将其更改为:

json = new JSONObject(DownloadText(url));

显然改变jsonJSONOBject

于 2012-04-24T21:31:02.143 回答
1

尝试

JSONObject object = new JSONObject (DownloadText(url));
                     user.id = object.getString("id");   
               user.name = object.getString("name");  
                user.first_name = object.getString("first_name"); 
                 user.last_name = object.getString("last_name"); 
                 user.link = object.getString("link");         
         user.username = object.getString("username");        
          user.gender = object.getString("gender");            
      user.locale = object.getString("locale");  
于 2012-04-24T21:37:30.197 回答
0

看起来pit.opensource.readjson.D0ReadJSONWebServiceActivity.pasreJSON正在尝试将 facebook 用户信息响应解码为一种JSONArray类型。

您应该将其存储在 a 中JSONObject

于 2012-04-24T21:22:14.283 回答