doc_m
doc_m

Reputation: 53

Trust Anchor for Certification path not found - https POST Request with Volley not working

this question is asked very often, but all the answers 2 or 3 years old. I have a login for my Android app and the date is saved in a MySQL database on my cloud server which has a SSL certificate. When I test my app on my local machine everything is fine, but when I try to connect with my cloud server I get the message "Trust Anchor for Certification path not found". My credentials are ok. I know that I have to set a sslSocketFactory. I tried so many, but one worked. I´m sitting now for days. May someone had a idea how to solve

here my code without sslSocketFactory

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.logintest, container, false);

        btn_login = view.findViewById(R.id.btn_logintest);
        email = view.findViewById(R.id.etEmail);
        password = view.findViewById(R.id.etPassword);

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }

            public void login() {

                str_email = email.getText().toString();
                str_password = password.getText().toString();

                if(!str_email.equals("") && !str_password.equals("")) {

                    StringRequest request = new StringRequest(Request.Method.POST, URL_LOGIN, new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            
                            FragmentManager fragmentManager = getFragmentManager();
                            FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
                            DummyFragment dummyFragment = new DummyFragment();
                            fragmentTransaction.replace(R.id.container,dummyFragment);
                            fragmentTransaction.addToBackStack(null);
                            fragmentTransaction.commit();

                            Log.e("Text: ", response);
                            Toast.makeText(getActivity(), "erfolgreicher Text: " +response, Toast.LENGTH_LONG).show();
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(getActivity(), "Text: " +error.getMessage().toString(), Toast.LENGTH_SHORT).show();
                        }
                    }
                    ) {
                        @Override
                        protected Map<String, String> getParams() throws AuthFailureError {
                            Map<String, String> params = new HashMap<>();
                            params.put("str_email", email.getText().toString());
                            params.put("str_password", password.getText().toString());
                            return params;
                        }

                        @Override
                        public Map<String, String> getHeaders() throws AuthFailureError {
                            try {
                            HashMap<String, String> headers = new HashMap<>();
                                String credentials = "xxxxxxxxx:xxxxxxxxxx";
                                String auth = "Basic "
                                        + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
                            headers.put("Content-Type", "application/json");
                            headers.put("Authorization", auth);
                            return headers;
                        } catch (Exception e) {
                                Log.e(TAG, "Authentication failure");
                                Toast.makeText(getActivity(), "" +e, Toast.LENGTH_LONG).show();
                            }
                            return super.getHeaders();
                        }
                    };
                    RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
                    requestQueue.add(request);
                }
                else {
                    if (email.getText().toString().equals("")) {
                        Toast.makeText(getActivity(), "Bitte Email Adresse eingeben", Toast.LENGTH_SHORT).show();
                    } else if (password.getText().toString().equals("")) {
                        Toast.makeText(getActivity(), "Bitte Passwort eingeben", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        return view;
    }
}

I tried the code from android developer page, but some error. I tried to set network_security.xml but it didn't worked for me. When I test my credentials with postman I get response 200.

Upvotes: 2

Views: 161

Answers (0)

Related Questions