0
のは、私はFacebookのGraphAPIを使用していますし、 hereを実証されたように、私は GraphRequest.executeAndWait()を使用して、次のページからの結果をロードしようとしているが、アプリ)(「android.os.NetworkOnMainThreadException」を

取得します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

いくつかのGoogle検索の後、私は解決策を得ました。

私はこのようなThreadGraphRequest.executeAndWait()コードラップ:

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

} .start(右のアプローチではありません)。

これは、新しいスレッドを作成することで実現しています。すべてのネットワーク操作は、新しいスレッドを作成するのではなく、すでに存在するバックグラウンドスレッドで実行する必要があります。これを行うには

方法はメインスレッド上でコードを実行するためにハンドラオブジェクトを使用しています。

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); 
関連する問題