0

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!

galch
  • 35
  • 6

1 Answers1

0

NetworkOnMainThreadException Maybe try not to do network on the main thread. see for reference

Getting the click event

  public void onClick(View v) {
  new Thread(new Runnable() {
    public void run() {
      final Bitmap b = loadImageFromNetwork();
      mImageView.post(new Runnable() {
        public void run() {
          mImageView.setImageBitmap(b);
        }
      });
    }
  }).start();
}

The goal of AsyncTask is to take care of thread management for you. Our previous example can easily be rewritten with AsyncTask:

public void onClick(View v) {
  new DownloadImageTask().execute("http://example.com/image.png");
}

private class DownloadImageTask extends AsyncTask {
     protected Bitmap doInBackground(String... urls) {
         return loadImageFromNetwork(urls[0]);
     }

     protected void onPostExecute(Bitmap result) {
         mImageView.setImageBitmap(result);
     }
 }
Anuj.T
  • 1,598
  • 16
  • 31
Nestor
  • 16
  • Please don't just give the reference to the website. Link may get expired or invalid. – Anuj.T Feb 20 '18 at 09:20
  • Thanks a lot for the answer! I would be happy if you could explain to me why a simple operation such as making a login that appears on the main screen needs another thread because nothing runs in the app until the connection is done. And is there any other solution besides going out to another thread? – galch Feb 22 '18 at 14:33