Search code examples
android-emulatorretrofitsslhandshakeexception

Emulator gives failure response: Trust anchor for certification path not found


I tried to search for this error a lot did not found any solution for it.

Details:

Made a request to API through retrofit, getting success response for the real devices but getting failure response if request is been made from android emulator.

public void loadPostsToDB() {
    Call<List<Post>> call = mAPIServiceInterface.getAllPosts();
    call.enqueue(new Callback<List<Post>>() {
      @Override
      public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
        List<Post> posts = response.body();
        new InsertPostAsyncTask(mDaoInterface,posts).execute();
      }

      @Override
      public void onFailure(Call<List<Post>> call, Throwable t) {
        Log.e("Failure Reason : "," "+t.toString());
      }
    });
  }

In above code gives response, I checked through debugging, as per it, point hits on success when ran from real device but failure function debug point got hit if ran from emulator.

Below is the log which got printed.

2020-12-01 20:57:23.647 6863-6879/reference.module.retrofit D/OkHttp: --> GET https://jsonplaceholder.typicode.com/posts
2020-12-01 20:57:23.647 6863-6879/reference.module.retrofit D/OkHttp: --> END GET
2020-12-01 20:57:23.828 6863-6882/reference.module.retrofit I/OpenGLRenderer: Initialized EGL, version 1.4
2020-12-01 20:57:23.828 6863-6882/reference.module.retrofit D/OpenGLRenderer: Swap behavior 1
2020-12-01 20:57:23.829 6863-6882/reference.module.retrofit W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2020-12-01 20:57:23.829 6863-6882/reference.module.retrofit D/OpenGLRenderer: Swap behavior 0
2020-12-01 20:57:23.849 6863-6882/reference.module.retrofit D/EGL_emulation: eglCreateContext: 0x9ca04240: maj 2 min 0 rcv 2
2020-12-01 20:57:23.924 6863-6882/reference.module.retrofit D/EGL_emulation: eglMakeCurrent: 0x9ca04240: ver 2 0 (tinfo 0x9ca037d0)
2020-12-01 20:57:24.135 6863-6882/reference.module.retrofit D/EGL_emulation: eglMakeCurrent: 0x9ca04240: ver 2 0 (tinfo 0x9ca037d0)
2020-12-01 20:57:24.278 6863-6879/reference.module.retrofit D/OkHttp: <-- HTTP FAILED: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
2020-12-01 20:57:24.279 6863-6863/reference.module.retrofit E/Failure Reason ::  javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

Any suggestion? what might be causing this behaviour?


Solution

  • The issue got solved, It was required change from server side for certificates.