2014-03-26 44 views
0

所以我試圖從facebook api請求所有的名字和所有的照片鏈接。 但我不斷收到錯誤,因爲異步任務已在運行。我想請求facebook api上的多個頁面名稱和頁面照片

public void getPages(){ 
    AsyncTask<String, Void, String[]> GL = j.new GetLikes().execute(extras.getString("PIC")); 

     try { 
      likes = GL.get(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ExecutionException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    for(int i=0;i<Json.PI;i++){ 
     final int j = i; 
    final Request photo = new Request(
      null, 
      "/" + PhotoId, 
      null, 
      HttpMethod.GET, 
      new Request.Callback() { 
       public void onCompleted(Response response) { 

        GraphObject graphObject = response.getGraphObject(); 
        JSONObject jsonObject = graphObject.getInnerJSONObject(); 

        try { 
         PageInfo[j][1] = (String) jsonObject.get("picture"); 

         Log.i("PICTURE", PageInfo[j][1]); 
        } catch (JSONException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 




       } 
      } 
     ); 

    final Request photos = new Request(
      null, 
      "/"+PageId+"/photos/uploaded", 
      null, 
      HttpMethod.GET, 
      new Request.Callback() { 
       public void onCompleted(Response response) { 

        GraphObject graphObject = response.getGraphObject(); 
        JSONObject jsonObject = graphObject.getInnerJSONObject(); 
        try { 
         JSONArray array = jsonObject.getJSONArray("data"); 
         JSONObject object = (JSONObject) array.get(0); 
         Log.i("PHOTOID", (String) object.get("id")); 
         PhotoId = (String) object.get("id"); 

        } catch (JSONException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        Log.i("PNAME", String.valueOf(response)); 

        photo.executeAsync(); 

       } 
      } 
     ); 

    Request PageID = new Request(
      null, 
      "/"+PageId, 
      null, 
      HttpMethod.GET, 
      new Request.Callback() { 
       public void onCompleted(Response response) { 
        GraphObject graphObject = response.getGraphObject(); 
        JSONObject jsonObject = graphObject.getInnerJSONObject(); 

        try { 
         PageInfo[j][0] = jsonObject.getString("name"); 

         Log.i("PNAME", PageInfo[j][0]); 

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

        photos.executeAsync(); 

       } 

      } 
     ); 
    PageID.executeAsync(); 

    } 


} 

我敢肯定即時通訊做得這一切都是錯誤的,我該如何處理這個問題?

回答

1
Request req = new Request(
session, 
"/{page-id}/feed", 
null, 
HttpMethod.GET, 
new Request.Callback(){ 
public void onCompleted(Response response) { 

       try 
     {  
      GraphObject go = response.getGraphObject(); 
      JSONObject jso = go.getInnerJSONObject(); 
      JSONArray arr = jso.getJSONArray("data"); 
      String name = " "; 
      String picture = " "; 

      for (int i = (arr.length()-1); i >=0; i--) 
      { 
       JSONObject json_obj = arr.getJSONObject(i); 


       name = json_obj.getString("name"); 
       picture = json_obj.getString("picture"); 
       //... 

      } 
      txtMessage.setText(name); 
      //imgFeed is ImageView 


      Picasso.with(getActivity()).load(picture).into(imgFeed); 

     } 
     catch (Throwable t) 
     { 
      t.printStackTrace(); 
     } 
    } 
});req.executeAsync(); 

這將從牆上的帖子獲得名稱和圖像......還請記住下載畢加索庫並將其添加到您的項目中。

希望這有助於