exception when trying to retrieve user's friends
-
21-12-2019 - |
Question
I'm trying to get on login action user's friends list with the following code:
Request request = Request.newMyFriendsRequest(ParseFacebookUtils.getSession(), new Request.GraphUserListCallback() {
@Override
public void onCompleted(List<GraphUser> users, Response response) {
// not interesting logic
}
});
request.executeAndWait();
and after I'm calling a function that runs the code I'm getting the following exception:
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)
Why does it happens, and how can I get user friends list without making it as Async task?
Solution
Instead of
request.executeAndWait();
try using
request.executeAsync();
Reference here.
Basically executeAndWait()
must be called from a background thread while executeAsync()
can be called from the UI thread.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow