我正在编写的应用程序需要来自Facebook的用户数据。当我可以从Facebook检索照片的问题时,我正在使用GraphRequest。我通过找到如何使用URL和HttpURLConnection提取位图来解决它。然后,当我提出要求时,出现错误NetworkOnMainThreadException。
我想从Facebook提取数据到我的应用程序UI。我要求使用Graph请求方法提供信息。我收到数据,直到需要来自Facebook的位图为止。我找到了使用HttpURLConnection的解决方案,但出现了NetworkOnMainThreadException。我查找了多个解决方案,说我需要执行ASYNC任务。我以为graphrequest是ASYNC任务,所以我不知道下一步该怎么做。
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.front_page);
android.support.v7.widget.Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_front_page);
NavigationView nV = findViewById(R.id.drawer_view);
View header = nV.getHeaderView(0);
nV.setNavigationItemSelectedListener(this);
callbackManager = CallbackManager.Factory.create();
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new VenueList()).commit();
nV.setCheckedItem(R.id.list_venue);
ImageView drawerProfilePicture = header.findViewById(R.id.drawer_pic_profile);
TextView drawerUserName = header.findViewById(R.id.drawer_profile_username);
TextView drawerUserEmail = header.findViewById(R.id.drawer_profile_email);
drawerUserName.setText("Hello My Baby");
drawerUserEmail.setText("myemail@gmail.com");
if (AccessToken.getCurrentAccessToken() != null && !AccessToken.getCurrentAccessToken().isExpired()) {
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), (object, response) -> {
try {
userFullName = object.getString("name");
// userFullName = object.getString("first_name") + " " + object.getString("last_name");
//userEmail = object.getString("email");
drawerUserName.setText(userFullName);
//drawerUserEmail.setText(userEmail);
String id = object.getString("id");
/* URL imageURL = new URL("https://graph.facebook.com/" + id + "/picture?type=large");
Bitmap bitmap = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());*/
URL url = new URL("https://graph.facebook.com/" + id + "/picture?type=large");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
drawerProfilePicture.setImageBitmap(myBitmap);
/* userProfilePicURL = "https://graph.facebook.com/" + id + "/pictures?type=normal";
Glide.with(FrontPageClass.this).load(userProfilePicURL).into(drawerProfilePicture);*/
Log.i(TAG, "name from Success: " + userFullName);
Log.i(TAG, "email from Sucess: " + userEmail);
} catch (JSONException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
});
Bundle parameters = new Bundle();
request.setParameters(parameters);
request.executeAsync();
}
Log.i(TAG, "onCreate: " + userFullName);
}
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vitaliy.changethename, PID: 14279
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1513)
at com.android.org.conscrypt.Platform.blockGuardOnNetwork(Platform.java:415)
at com.android.org.conscrypt.ConscryptFileDescriptorSocket.shutdownAndFreeSslNative(ConscryptFileDescriptorSocket.java:1005)
at com.android.org.conscrypt.ConscryptFileDescriptorSocket.close(ConscryptFileDescriptorSocket.java:1000)
at com.android.okhttp.internal.Util.closeQuietly(Util.java:86)
at com.android.okhttp.internal.http.StreamAllocation.deallocate(StreamAllocation.java:256)
at com.android.okhttp.internal.http.StreamAllocation.connectionFailed(StreamAllocation.java:293)
at com.android.okhttp.internal.http.HttpEngine.close(HttpEngine.java:445)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:509)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:244)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:26)
at com.example.vitaliy.changethename.FrontPageClass.lambda$onCreate$0$FrontPageClass(FrontPageClass.java:94)
at com.example.vitaliy.changethename.-$$Lambda$FrontPageClass$TF1liEC2dHjoBVyqHtUi2sJJ-4A.onCompleted(Unknown Source:6)
at com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:316)
at com.facebook.GraphRequest$5.run(GraphRequest.java:1395)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
我想对从我写入的URL中获得的位图进行dsiplay。我得到了NetworkOnMainThreadException,但我不明智地假设GraphRequest是一个异步进程。我不知道下一步该怎么修复代码。