走走android.os.NetworkOnMainThreadException“同时的AsyncTask
的执行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
处理,但这不是这种情况。
请让我知道是什么原因导致这个错误以及如何摆脱它。
经过几次谷歌搜索,我得到了解决方案。
我只是包裹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();
public void onCompleted()
在UI线程上运行,无论哪个线程调用此请求。因此,如果您想在public void onCompleted()
内执行另一个请求,则必须将其包装到另一个AsyncTask
或其他异步机制中。
好的!所以,如果我写另一个'AsyncTask'。我必须传递'request'作为参数。我如何使用AsyncTask来做到这一点? –
这不是正确的方法
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);
请张贴异常堆栈跟踪,太。 – laalto
@laalto请参阅最新的问题 –
最内层的'onCompleted()'回调正在主线程上运行,并且您正在执行'executeAndWait()'。 –