@Nullix

Как решить проблему подключиния к серверу на Android API 20 и ниже?

Приложение обращается к php файлу на сервере с помощью OkHttp. На Android API 20 и выше, все работает. Если API ниже 20, то возникает следующие ошибки:
spoiler
07-17 16:19:58.612 1509-1524/com.example.verum.optron001 W/System.err: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
07-17 16:19:58.612 1509-1524/com.example.verum.optron001 W/System.err:     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:381)
07-17 16:19:58.616 1509-1524/com.example.verum.optron001 W/System.err:     at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:302)
07-17 16:19:58.616 1509-1524/com.example.verum.optron001 W/System.err:     at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:270)
07-17 16:19:58.620 1509-1524/com.example.verum.optron001 W/System.err:     at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:162)
07-17 16:19:58.624 1509-1524/com.example.verum.optron001 W/System.err:     at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)
07-17 16:19:58.624 1509-1524/com.example.verum.optron001 W/System.err:     at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
07-17 16:19:58.624 1509-1524/com.example.verum.optron001 W/System.err:     at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
07-17 16:19:58.624 1509-1524/com.example.verum.optron001 W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
07-17 16:19:58.624 1509-1524/com.example.verum.optron001 W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
07-17 16:19:58.624 1509-1524/com.example.verum.optron001 W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
07-17 16:19:58.624 1509-1524/com.example.verum.optron001 W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
07-17 16:19:58.624 1509-1524/com.example.verum.optron001 W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
07-17 16:19:58.624 1509-1524/com.example.verum.optron001 W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
07-17 16:19:58.624 1509-1524/com.example.verum.optron001 W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
07-17 16:19:58.628 1509-1524/com.example.verum.optron001 W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
07-17 16:19:58.628 1509-1524/com.example.verum.optron001 W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
07-17 16:19:58.628 1509-1524/com.example.verum.optron001 W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
07-17 16:19:58.628 1509-1524/com.example.verum.optron001 W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
07-17 16:19:58.628 1509-1524/com.example.verum.optron001 W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
07-17 16:19:58.632 1509-1524/com.example.verum.optron001 W/System.err:     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147)
07-17 16:19:58.632 1509-1524/com.example.verum.optron001 W/System.err:     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
07-17 16:19:58.632 1509-1524/com.example.verum.optron001 W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-17 16:19:58.632 1509-1524/com.example.verum.optron001 W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-17 16:19:58.632 1509-1524/com.example.verum.optron001 W/System.err:     at java.lang.Thread.run(Thread.java:856)
07-17 16:19:58.632 1509-1524/com.example.verum.optron001 W/System.err: Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
07-17 16:19:58.632 1509-1524/com.example.verum.optron001 W/System.err:     at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:276)
07-17 16:19:58.636 1509-1524/com.example.verum.optron001 W/System.err:     at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:197)
07-17 16:19:58.636 1509-1524/com.example.verum.optron001 W/System.err:     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:584)
07-17 16:19:58.636 1509-1524/com.example.verum.optron001 W/System.err:     at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
07-17 16:19:58.636 1509-1524/com.example.verum.optron001 W/System.err:     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:378)
07-17 16:19:58.636 1509-1524/com.example.verum.optron001 W/System.err: 	... 23 more
07-17 16:19:58.636 1509-1524/com.example.verum.optron001 W/System.err: Caused by: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
07-17 16:19:58.636 1509-1524/com.example.verum.optron001 W/System.err: 	... 28 more

Может кто сталкивался с подобным?
Код который выполняет подключение к серверу:
spoiler
public  void signInClick(View view) {

        numberField = findViewById(R.id.numberPhone);
        passwordField = findViewById(R.id.password);

        numberPhone = numberField.getText().toString();
        String password = passwordField.getText().toString();

        /*String hostname = "000webhostapp.com";

        CertificatePinner certificatePinner = new CertificatePinner.Builder()
                .add(hostname, "sha256/c95d249a927d79db828e18296aee11d211fb7d95327971277ff83b13f8d5b466=")
                .build();*/

        OkHttpClient client =  new OkHttpClient();;

        String url = "https://optron001.000webhostapp.com/signIn.php?numberPhone=" + numberPhone + "&password=" + password;
        //создаем запрос к серверу
        Request request = new Request.Builder().url(url).build();

        //выполняем запрос и получаем ответ
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                LoginActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {  showMessege("Отсутствует подключение к интеренету"); } });
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    final byte[] check = response.body().bytes();
                    LoginActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(check[0] == 49)
                                startMaps();
                            else if(check[0] == 50)
                                showMessege("Несуществующий пользователь");
                            else
                                showMessege("Неверный пароль");
                        }
                    });
                }
                else
                    LoginActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {  showMessege("Сервер недоступен"); } });
            }
        });
    }
  • Вопрос задан
  • 2058 просмотров
Пригласить эксперта
Ответы на вопрос 1
KuSu
@KuSu
Android разработчик
В логе написано, что проблема с ssl сертификатом. Или править надо сертификат на сервере, или отключать проверку ssl на устройстве.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы