0

I have a problem here, I'm using okhttp 3 to pass some parameters to my php script. It worked when i use http:// but when i change to https:// it always says error.

This is my log:

javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

This is portion of my code:

OkHttpClient client = new OkHttpClient();
            RequestBody requestBody = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("username", txtUsername.getText().toString())
                    .addFormDataPart("password", txtPassword.getText().toString())
                    .build();

            Request request = new Request.Builder()
                    .url(my_https_url)
                    .post(requestBody)
                    .build();

            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.e("Error", e.toString());
                    backgroundThreadShortToast(getApplication(), "Something Happened");
                }

                @Override
                public void onResponse(Call call, final Response response) throws IOException {
                    final String responseData = response.body().string();
                    //backgroundThreadShortToast(getApplication(), responseData);
                    LoginActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(responseData.equals("Login Failed")){
                                Toast.makeText(LoginActivity.this, "Username atau password salah", Toast.LENGTH_SHORT).show();
                                clickcount=clickcount+1;

                                if(clickcount==5)
                                {
                                    btnLogin.setEnabled(false);
                                    new Handler().postDelayed(new Runnable()
                                    {
                                        public void run()
                                        {
                                            btnLogin.setClickable(true);
                                            clickcount = 0;
                                        }
                                    }, 5000);

                                }
                            }else{
                                id_user = responseData;

                                checkVer();
                            }
                        }
                    });
                }
            });

When i run my code, it always go to onFailure. I have search some information in another article but i don't get the answer. Can somebody help me? Thanks for your help before.

Punit
  • 324
  • 1
  • 4
  • 17
Marcel Ruben
  • 81
  • 12
  • Yes, because when you communicate over https (which is secure version of hyper text transfer protocol) you need to provide certificate to prove you are who you say you are. It wouldn't be very secure otherwise ;-) I'm not sure where that goes in your code, but you need to send certifcate. – Thomas Cook Sep 26 '17 at 15:05
  • see: https://stackoverflow.com/questions/2642777/trusting-all-certificates-using-httpclient-over-https/6378872#6378872 – Thomas Cook Sep 26 '17 at 15:06
  • @ThomasCook thanks for your help – Marcel Ruben Sep 28 '17 at 04:28

0 Answers0