从android应用程序中的fql查询中提取信息

时间:2012-12-12 19:11:40

标签: android facebook facebook-fql

我正在尝试将我上传的视频的视频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:第25个字符的未终止对象   {响应:responseCode:200,graphObject:GraphObject {graphObjectClass = GraphObject,状态=   { “数据”:[{ “所有者”:...}]}}}

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

2 个答案:

答案 0 :(得分: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);                 
        }
    });

答案 1 :(得分: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"));              
       }

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