我试图使用以下代码登录登录操作用户的好友列表:
Request request = Request.newMyFriendsRequest(ParseFacebookUtils.getSession(), new Request.GraphUserListCallback() {
@Override
public void onCompleted(List<GraphUser> users, Response response) {
// not interesting logic
}
});
request.executeAndWait();
在我调用运行代码的函数后,我得到以下异常:
04-14 00:39:55.896 25106-25106/com.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.app, PID: 25106
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
at com.android.org.conscrypt.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:724)
at java.io.BufferedOutputStream.flushInternal(BufferedOutputStream.java:185)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:85)
at com.android.okhttp.internal.http.HttpTransport.flushRequest(HttpTransport.java:107)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:642)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:136)
at com.facebook.Response.fromHttpConnection(Response.java:301)
at com.facebook.Request.executeConnectionAndWait(Request.java:1565)
at com.facebook.Request.executeBatchAndWait(Request.java:1464)
at com.facebook.Request.executeBatchAndWait(Request.java:1433)
at com.facebook.Request.executeBatchAndWait(Request.java:1415)
at com.facebook.Request.executeAndWait(Request.java:1387)
at com.facebook.Request.executeAndWait(Request.java:1269)
at com.app.LoginActivity.getFriends(LoginActivity.java:187)
为什么会发生这种情况,如何在不将其作为异步任务的情况下获取用户好友列表?
答案 0 :(得分:1)
而不是
request.executeAndWait();
尝试使用
request.executeAsync();
参考here。
基本上必须从后台线程调用executeAndWait()
,而可以从UI线程调用executeAsync()
。