Y0gesh Gupta
Y0gesh Gupta

Reputation: 2204

Extracting information from a fql query in android app

I am trying to store the video id and other information of a video uploaded by me in different strings in android. Right now I have created a fql query to get the video details. I am using json parsing to extract the values like this-

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);                 
                }
            });

But its throwing me exception-

org.json.JSONException:Unterminated object at character 25 of {Response:responseCode:200,graphObject:GraphObject{graphObjectClass=GraphObject,state= {"data":[{"owner":...}]}}}

Its my first time with android,facebook sdk and also with json parsing, so I will be really greatfull for any help provided.Thanks.

Upvotes: 3

Views: 1873

Answers (2)

Benny Kong
Benny Kong

Reputation: 31

    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);                 
        }
    });

Upvotes: 3

Y0gesh Gupta
Y0gesh Gupta

Reputation: 2204

Well finally I was able to get the problem here, response.toString() was not giving a pure JSONObject string so I had to take the substring from the output of response.toString() to make it look like a JSONObject string.

       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"));              
       }

I guess it might not be the proper way to do it, but this is the only way I could come up with. If any one knows a better way to do it kindly reply.Thanks

Upvotes: 1

Related Questions