0

的執行GraphRequest.executeAndWait()在doInBackground()我使用Facebook的GraphAPI,我試圖加載使用GraphRequest.executeAndWait()作爲證明here翻頁效果,但應用程序正在崩潰給0​​。走走android.os.NetworkOnMainThreadException「同時的AsyncTask

這裏是我的代碼:

class RetrieveF extends AsyncTask<Void, Integer, String> { 

      GraphRequest request; 

      protected String doInBackground(Void...args0) { 
       new GraphRequest(
         AccessToken.getCurrentAccessToken(), "/me/friends", null, HttpMethod.GET, 
         new GraphRequest.Callback() { 
          public void onCompleted(GraphResponse response) { 
           JSONObject innerJson = response.getJSONObject(); 
           try { 
            JSONArray data = innerJson.getJSONArray("data"); 
            for (int i = 0; i<data.length(); i++){ 

             String id = data.getJSONObject(i).getString("id"); 

             request = new GraphRequest(AccessToken.getCurrentAccessToken(), id+"/feed", null, HttpMethod.GET, 
               new GraphRequest.Callback() { 
                @Override 
                public void onCompleted(GraphResponse response) { 

                 JSONObject innerJson = response.getJSONObject(); 
                 try { 
                  JSONArray data = innerJson.getJSONArray("data"); 
                  for (int i = 0; i<data.length(); i++) { 
                   JSONObject obj = data.getJSONObject(i);} 
                 } catch (JSONException e) { 
                  Log.d("innerFacebookException", e.getMessage()); 
                 } 

                 GraphRequest nextResultsRequests = response.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT); 
                 if (nextResultsRequests != null) { 
                  nextResultsRequests.setCallback(request.getCallback()); 
                  // error on this line 
                  response = nextResultsRequests.executeAndWait(); 
                  // 
                 } 
                } 
               } 
             ); 
             request.executeAsync(); 
            } 
           } catch (JSONException e) { 
            Log.d("facebookException", e.getMessage()); 
           } 
          } 
         } 
       ).executeAsync(); 
       return "success"; 
      } 

      @Override 
      protected void onPostExecute(String s) { 
       super.onPostExecute(s); 
      } 
     } 

這裏的堆棧跟蹤:

android.os.NetworkOnMainThreadException 
                     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273) 
                     at com.android.org.conscrypt.OpenSSLSocketImpl.shutdownAndFreeSslNative(OpenSSLSocketImpl.java:1131) 
                     at com.android.org.conscrypt.OpenSSLSocketImpl.close(OpenSSLSocketImpl.java:1126) 
                     at com.android.okhttp.Connection.closeIfOwnedBy(Connection.java:132) 
                     at com.android.okhttp.OkHttpClient$1.closeIfOwnedBy(OkHttpClient.java:75) 
                     at com.android.okhttp.internal.http.HttpConnection.closeIfOwnedBy(HttpConnection.java:137) 
                     at com.android.okhttp.internal.http.HttpTransport.disconnect(HttpTransport.java:135) 
                     at com.android.okhttp.internal.http.HttpEngine.disconnect(HttpEngine.java:578) 
                     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.disconnect(HttpURLConnectionImpl.java:122) 
                     at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.disconnect(DelegatingHttpsURLConnection.java:93) 
                     at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.disconnect(HttpsURLConnectionImpl.java) 
                     at com.facebook.internal.Utility.disconnectQuietly(Utility.java:416) 
                     at com.facebook.GraphRequest.executeConnectionAndWait(GraphRequest.java:1272) 
                     at com.facebook.GraphRequest.executeBatchAndWait(GraphRequest.java:1168) 
                     at com.facebook.GraphRequest.executeBatchAndWait(GraphRequest.java:1134) 
                     at com.facebook.GraphRequest.executeBatchAndWait(GraphRequest.java:1118) 
                     at com.facebook.GraphRequest.executeAndWait(GraphRequest.java:1093) 
                     at com.facebook.GraphRequest.executeAndWait(GraphRequest.java:987) 
                     at com.qbc.xxx.MainActivity$RetrieveFacebookPosts$1$1.onCompleted(MainActivity.java:398) 
                     at com.facebook.GraphRequest$5.run(GraphRequest.java:1383) 
                     at android.os.Handler.handleCallback(Handler.java:739) 
                     at android.os.Handler.dispatchMessage(Handler.java:95) 
                     at android.os.Looper.loop(Looper.java:148) 
                     at android.app.ActivityThread.main(ActivityThread.java:5417) 
                     at java.lang.reflect.Method.invoke(Native Method) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)    

我無法弄清楚是什麼導致這個錯誤的這種錯誤通常導致當這種代碼是不在AsyncTask處理,但這不是這種情況。

請讓我知道是什麼原因導致這個錯誤以及如何擺脫它。

+0

請張貼異常堆棧跟蹤,太。 – laalto

+0

@laalto請參閱最新的問題 –

+0

最內層的'onCompleted()'回調正在主線程上運行,並且您正在執行'executeAndWait()'。 –

回答

0

經過幾次谷歌搜索,我得到了解決方案。

我只是包裹GraphRequest.executeAndWait()代碼在Thread這樣的:

Thread t = new Thread() { 
    @Override 
    public void run() { 
     GraphRequest nextResultsRequests = lastGraphResponse.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT); 
     if (nextResultsRequests != null) { 
      nextResultsRequests.setCallback(request.getCallback()); 
      lastGraphResponse = nextResultsRequests.executeAndWait(); 
     } 
    } 
}; 
t.start(); 
1

public void onCompleted()在UI線程上運行,無論哪個線程調用此請求。因此,如果您想在public void onCompleted()內執行另一個請求,則必須將其包裝到另一個AsyncTask或其他異步機制中。

+0

好的!所以,如果我寫另一個'AsyncTask'。我必須傳遞'request'作爲參數。我如何使用AsyncTask來做到這一點? –

1

這不是正確的方法

Thread t = new Thread() { 
@Override 
public void run() { 
    GraphRequest nextResultsRequests = lastGraphResponse.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT); 
    if (nextResultsRequests != null) { 
     nextResultsRequests.setCallback(request.getCallback()); 
     lastGraphResponse = nextResultsRequests.executeAndWait(); 
    } 
} 

}。開始();

你正在通過創建一個新的線程來做到這一點。所有的網絡操作應該在後臺線程已經存在,而不是通過創建一個新的線程。

做到這一點的方法是使用處理對象上主線程運行代碼。

Handler handler = new Handler(); 
    handler.post(new Runnable() { 
     @Override 
     public void run() { 
      GraphRequest nextResultsRequests = lastGraphResponse.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT); 
      if (nextResultsRequests != null) { 
       nextResultsRequests.setCallback(request.getCallback()); 
       lastGraphResponse = nextResultsRequests.executeAndWait(); 
      } 
     } 
    }); 

或者你可以有一些延遲後做了手術,通過使用postDelayed

handler.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      GraphRequest nextResultsRequests = lastGraphResponse.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT); 
      if (nextResultsRequests != null) { 
       nextResultsRequests.setCallback(request.getCallback()); 
       lastGraphResponse = nextResultsRequests.executeAndWait(); 
      } 
     } 
    }, DELAY_IN MILLISECONDS);