3

我正在尝试将我上传的视频的视频 ID 和其他信息存储在 android 中的不同字符串中。现在我已经创建了一个 fql 查询来获取视频详细信息。我正在使用 json 解析来提取这样的值-

String fqlQuery = "SELECT vid, owner, title, description,updated_time, created_time FROM    video WHERE owner=me()";
                    Bundle params = new Bundle();
                    params.putString("q", fqlQuery);
                    Session session = Session.getActiveSession();
                    Request request = new Request(session,"/fql",params,HttpMethod.GET,new Request.Callback()
                    {
                        public void onCompleted(Response response)
                        {
                            try
                            {
                                JSONObject json = Util.parseJson(response.toString());
                                JSONArray data = json.getJSONArray( "data" );
                                for ( int i = 0, size = data.length(); i < size; i++ )
                                {
                                    JSONObject getVideo = data.getJSONObject( i );
                                    userNameView.setText(getVideo.getString("vid"));

                                }

                            }
                            catch(Exception e){userNameView.setText(e.toString());}
                        }                  
                    }); 
                    Request.executeBatchAsync(request);                 
                }
            });

但它让我异常 -

org.json.JSONException: {Response:responseCode:200,graphObject:GraphObject{graphObjectClass=GraphObject,state= {"data":[{"owner":...}]}}} 字符 25 处的未终止对象

这是我第一次使用 android、facebook sdk 以及 json 解析,所以我会非常感谢提供的任何帮助。谢谢。

4

2 回答 2

3
    queryButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String fqlQuery = "SELECT uid, name, pic_square, status FROM user WHERE uid IN " +
                  "(SELECT uid2 FROM friend WHERE uid1 = me() LIMIT 25)";
            Bundle params = new Bundle();
            params.putString("q", fqlQuery);
            Session session = Session.getActiveSession();

            Request request = new Request(session,
                "/fql",                         
                params,                         
                HttpMethod.GET,                 
                new Request.Callback(){         

                    @SuppressWarnings("deprecation")
                    public void onCompleted(Response response) {

                            GraphObject graphObject = response.getGraphObject();


                            if (graphObject != null)
                            {
                                if (graphObject.getProperty("data") != null)
                                {
                                    try {
                                        String arry = graphObject.getProperty("data").toString();

                                        JSONArray jsonNArray = new JSONArray(arry);

                                        for (int i = 0; i < jsonNArray.length(); i++) {

                                            JSONObject jsonObject = jsonNArray.getJSONObject(i);

                                            String name = jsonObject.getString("name");
                                            String uid = jsonObject.getString("uid");

                                            String pic_square = jsonObject.getString("pic_square");
                                            String status = jsonObject.getString("status");

                                            Log.i("Entry", "uid: " + uid + ", name: " + name + ", pic_square: " + pic_square + ", status: " + status);
                                        }

                                    } catch (JSONException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }                                       
                                }
                            }

                    }                  
            }); 
            Request.executeBatchAsync(request);                 
        }
    });
于 2012-12-29T07:15:44.957 回答
1

好吧,最后我在这里找到了问题,response.toString() 没有给出纯 JSONObject 字符串,所以我不得不从 response.toString() 的输出中获取子字符串,使它看起来像一个 JSONObject 字符串。

       int indexex=nthOccurrence(response.getGraphObject().toString(),'{',1);
       int index=response.getGraphObject().toString().length()-1;
       String edit=response.getGraphObject().toString().substring(indexex, index);
       JSONObject json = Util.parseJson(edit);
       JSONArray data = json.getJSONArray( "data" );
       for ( int i = 0, size = data.length(); i < size; i++ )
       {
          JSONObject getVideo = data.getJSONObject( i );
          userNameView.setText(getVideo.getString("vid"));              
       }

我想这可能不是正确的方法,但这是我能想到的唯一方法。如果有人知道更好的方法,请回复。谢谢

于 2012-12-13T05:20:10.860 回答