Android really doesn’t want you to do anything on the main activity thread except render the UI. If you do something else on that thread, and whatever that is has the potential to take some time to complete, then your app’s interface can appear to be non-responsive. To help ensure that doesn’t happen, Android’s JVM will complain if it detects that you’re trying to do something that has the potential to take a while on the main activity thread, and in this case that is network activity.
The best way to avoid this is to ensure that all processing not related to updating the UI is done in one or more other threads. One way to accomplish this is to put that code in a class that extends AsyncTask and then call the task’s execute() method to ensure that the processing happens in a background thread. See https://developer.android.com/training/articles/perf-anr.html for a good overview of this.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I want use LDAP in android studio, I use UnboundID LDAP SDK for Java in the latest version.
I use the command:
LDAPConnection ldap = new LDAPConnection("xxx.xxx.xx.xx", 389,"uid=guest3,ou=Users,dc=gal,dc=local", guest3);
Connection details:
host: xxx.xxx.xx.xx
port: 389
dn: uid=guest3,ou=Users,dc=gal,dc=local
password: guest3
But When I try to connect to the LDAP server, I get the following error:
LDAPException(resultCode=82 (local error), errorMessage='An error occurred while encoding the LDAP message or sending it to server xx.xxx.xx.xx:389: NetworkOnMainThreadException(trace='onNetwork(StrictMode.java:1303) / socketWrite(SocketOutputStream.java:111) / write(SocketOutputStream.java:157) / flushBuffer(BufferedOutputStream.java:82) / flush(BufferedOutputStream.java:140) / sendMessage(LDAPConnectionInternals.java:580) / sendMessage(LDAPConnection.java:4375) / process(SimpleBindRequest.java:556) / bind(LDAPConnection.java:2270) / bind(LDAPConnection.java:2215) / onClick(LoginActivity.java:57) / performClick(View.java:5610) / run(View.java:22265) / handleCallback(Handler.java:751) / dispatchMessage(Handler.java:95) / loop(Looper.java:154) / main(ActivityThread.java:6077) / invoke(Method.java:native) / run(ZygoteInit.java:866) / main(ZygoteInit.java:756)', ldapSDKVersion=4.0.4, revision=27051)')
If anyone know how to remedy this issue, it would be greatly appreciated. Thanks in advance!
Android really doesn’t want you to do anything on the main activity thread except render the UI. If you do something else on that thread, and whatever that is has the potential to take some time to complete, then your app’s interface can appear to be non-responsive. To help ensure that doesn’t happen, Android’s JVM will complain if it detects that you’re trying to do something that has the potential to take a while on the main activity thread, and in this case that is network activity.
The best way to avoid this is to ensure that all processing not related to updating the UI is done in one or more other threads. One way to accomplish this is to put that code in a class that extends AsyncTask and then call the task’s execute() method to ensure that the processing happens in a background thread. See https://developer.android.com/training/articles/perf-anr.html for a good overview of this.