2012-12-12 33 views
3

我正尝试在Android中的不同字符串中存储由我上传的视频的视频ID和其他信息。现在我已经创建了一个fql查询来获取视频细节。我使用JSON解析提取喜欢这个 -在Android应用程序中从fql查询中提取信息

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

的值,但它扔我exception-

org.json.JSONException:未终止的对象在 {响应特性25:responseCode:200 ,graphObject:GraphObject {graphObjectClass = GraphObject,状态= { “数据”:[{ “所有者”:...}]}}}

它的我第一次与Android,Facebook的SDK也使用JSON解析,所以我会真的伟大的任何帮助提供。谢谢。

回答

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

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

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