Question

I've got Problems on API 15 + 16, for 17 I don't know, but I know that on 18 and 19 it is working.

main.java:

...
    // Prozess/Thread für Internetverbindung/Daten abrufen
private class MyThread extends Thread {
    @Override
    public void run() {

        if (stop) {
            if (DEBUG) {
                Log.d(TAG, "Stop ist true");
            }
            // nicht weitermachen, da erst der User unter Optionen
            // die
            // Einstellungen ändern muss
        } else {
            int TIMEOUT_VALUE = 3000;
            for (int z = 0; z < datum.length; z++) {
                Resultat = null;
                Endres = null;
                if (DEBUG) {
                    Log.d(TAG, "Endres " + Endres + " " + Resultat);
                    Log.d(TAG, "z: " + z);
                }

                statusZaehler = statusZaehler + 100 / statusZ;
                if (DEBUG) {
                    Log.d(TAG, o + " thread start statusZaehler "
                            + statusZaehler);
                }
                o++;

                if (DEBUG) {
                    Log.d(TAG, "run1");
                }

                // try {
                if (DEBUG) {
                    Log.d(TAG, "run2");
                }
                // ### Datumseingabe bzw. Variable
                pruefdatum = datum[z];

                if (DEBUG) {
                    Log.d(TAG, "Prüfdatum: " + pruefdatum);
                }

                // ### URL Abfragen & Auslesen
                // URL url = null;

                if (!NetworkInfo(getBaseContext())) {
                    showDialog(DIALOG_ALERT);
                    progressThread.setState(ProgressThread.STATE_DONE);
                    stop1 = true;
                    break;
                } else {

                    // Datenabruf

                    // try {

                    if (DEBUG) {
                        Log.d(TAG, "Daten aus dem Internet abrufen "
                                + pruefdatum);
                    }

                    javax.net.ssl.SSLContext sslContext = null;
                    javax.net.ssl.SSLSocketFactory socketFactory = null;
                    try {

                        sslContext = SSLContext.getInstance("TLS"); // (1)
                        sslContext
                                .init(null,
                                        new TrustManager[] { de.mdisco.ssl.TrustManagerFactory
                                                .get() },
                                        new SecureRandom()); // (2)
                        socketFactory = sslContext.getSocketFactory();
                    } catch (Exception e) {
                        Log.e(TAG,
                                "Error in ssl connection " + e.toString());
                    }

                    HttpsURLConnection
                            .setDefaultSSLSocketFactory(socketFactory); // (3)
                    HttpsURLConnection
                            .setDefaultHostnameVerifier(new de.mdisco.ssl.TestX509HostnameVerifier()); // (4)

                    HttpsURLConnection httpsUrlConnection = null;

                    try {

                        httpsUrlConnection = (HttpsURLConnection) new URL(
                                "https://....."+ pruefdatum + "&asi=")
                                .openConnection();

                        httpsUrlConnection.setConnectTimeout(TIMEOUT_VALUE);
                        httpsUrlConnection.setReadTimeout(TIMEOUT_VALUE);

                        httpsUrlConnection.connect(); // (5)

                    } catch (ConnectTimeoutException e) {
                        Log.e("Timeout Exception: ", e.toString());
                        Log.d(TAG, "fehler: " + e.getMessage()
                                + " More than " + TIMEOUT_VALUE
                                + " elapsed.");
                        myProgressDialog.dismiss();
                        Connfehler();
                        stop1 = true;
                        break;
                    } catch (SocketTimeoutException ste) {
                        Log.e("Timeout Exception: ", ste.toString());
                        Log.d(TAG, "fehler: " + ste.getMessage()
                                + " More than " + TIMEOUT_VALUE
                                + " elapsed.");
                        myProgressDialog.dismiss();
                        Connfehler();
                        stop1 = true;
                        break;
                    } catch (MalformedURLException me) {
                        Log.d(TAG,
                                "fehler in Internetverbindung: "
                                        + me.getMessage());
                        myProgressDialog.dismiss();
                        Connfehler();
                        stop1 = true;
                        break; // finish();

                    } catch (IllegalStateException cause) {
                        Log.e(TAG,
                                "Error in https connection Illegal State "
                                        + cause.toString());

                    } catch (Exception e) {
                        Log.e(TAG,
                                "Error in https connection " + e.toString());
                    }
                    // HTML der Webseite auslesen:

                    String lesezeile = null;
                    try {
                        BufferedReader buffReader = new BufferedReader(
                                new InputStreamReader(httpsUrlConnection
                                        .getInputStream()),
                                8 * 1024);

                        while ((lesezeile = buffReader.readLine()) != null) {
                            if (Resultat == null) {

                                Resultat = lesezeile;

                            } else {

                                Resultat = Resultat + lesezeile;

                            }

                        }

                        if (DEBUG) {
                            Log.d(TAG, "Edatum " + pruefdatum);
                            Log.d(TAG, Resultat + "");
                        }
                        buffReader.close();
                    } catch (Exception e) {
                        Log.e(TAG,
                                "Error buffered Reader result "
                                        + e.toString());
                    }
                    httpsUrlConnection.disconnect();
                    Endres = Resultat;
                    statusZaehler = statusZaehler + 100 / statusZ;
                    if (DEBUG) {
                        Log.d(TAG, o + "thread Übergabe statusZaehler "
                                + statusZaehler);
                    }
                    o++;
                    parse();

                    /*
                     * } catch (MalformedURLException me) { Log.d(TAG,
                     * "fehler in Internetverbindung: " + me.getMessage());
                     * myProgressDialog.dismiss(); Connfehler(); stop1 =
                     * true; break; // finish(); }
                     */

                    if (DEBUG) {
                        Log.d(TAG, "Ende StartInternet");

                        Log.d(TAG, "parse+offline ende");
                    }

                }
                /*
                 * } catch (ConnectTimeoutException e) {
                 * Log.e("Timeout Exception: ", e.toString()); Log.d(TAG,
                 * "fehler: " + e.getMessage() + " More than " +
                 * TIMEOUT_VALUE + " elapsed."); myProgressDialog.dismiss();
                 * Connfehler(); stop1 = true; break; } catch
                 * (SocketTimeoutException ste) {
                 * Log.e("Timeout Exception: ", ste.toString()); Log.d(TAG,
                 * "fehler: " + ste.getMessage() + " More than " +
                 * TIMEOUT_VALUE + " elapsed."); myProgressDialog.dismiss();
                 * Connfehler(); stop1 = true; break; } catch (Exception e)
                 * { Log.e(TAG, "Error in http connection " + e.toString());
                 * 
                 * }
                 */
            }

        }
...

Error-Log:

04-25 23:05:17.562: E/Main(21301): Error in https connection java.lang.NullPointerException 04-25 23:05:17.562: W/System.err(21301): java.lang.NullPointerException 04-25 23:05:17.572: W/System.err(21301): at de.mdisco.ssl.TrustManagerFactory$MeinX509TrustManager.checkServerTrusted(TrustManagerFactory.java:35) 04-25 23:05:17.572: W/System.err(21301): at de.mdisco.ssl.TrustManagerFactory$MeinX509TrustManager.checkServerTrusted(TrustManagerFactory.java:35) 04-25 23:05:17.572: W/System.err(21301): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:573) 04-25 23:05:17.572: W/System.err(21301): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:573) 04-25 23:05:17.572: W/System.err(21301): at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method) 04-25 23:05:17.572: W/System.err(21301): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:371) 04-25 23:05:17.582: W/System.err(21301): at libcore.net.http.HttpConnection.setupSecureSocket(HttpConnection.java:209) 04-25 23:05:17.582: W/System.err(21301): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:478) 04-25 23:05:17.582: W/System.err(21301): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433) 04-25 23:05:17.582: W/System.err(21301): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289) 04-25 23:05:17.582: W/System.err(21301): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239) 04-25 23:05:17.582: W/System.err(21301): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80) 04-25 23:05:17.582: W/System.err(21301): at libcore.net.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:165) 04-25 23:05:17.582: W/System.err(21301): at de.mdisco.HWRapp.Main$MyThread.run(Main.java:1204) 04-25 23:05:17.582: E/Main(21301): Error buffered Reader result java.lang.NullPointerException 04-25 23:05:17.572: W/System.err(21301): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:573) 04-25 23:05:17.572: W/System.err(21301): at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method) 04-25 23:05:17.572: W/System.err(21301): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:371) 04-25 23:05:17.582: W/System.err(21301): at libcore.net.http.HttpConnection.setupSecureSocket(HttpConnection.java:209) 04-25 23:05:17.582: W/System.err(21301): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:478) 04-25 23:05:17.582: W/System.err(21301): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433) 04-25 23:05:17.582: W/System.err(21301): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289) 04-25 23:05:17.582: W/System.err(21301): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239) 04-25 23:05:17.582: W/System.err(21301): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80) 04-25 23:05:17.582: W/System.err(21301): at libcore.net.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:165) 04-25 23:05:17.582: W/System.err(21301): at de.mdisco.HWRapp.Main$MyThread.run(Main.java:1204) 04-25 23:05:17.582: E/Main(21301): Error buffered Reader result java.lang.NullPointerException

So, I think a secure SSL connection can't be established.

But where is my error inside the code?

Thanks for helping!

Was it helpful?

Solution

04-25 23:05:17.562: E/Main(21301): Error in https connection
java.lang.NullPointerException 04-25 23:05:17.562: W/System.err(21301):
java.lang.NullPointerException 04-25 23:05:17.572: W/System.err(21301):
     at de.mdisco.ssl.TrustManagerFactory$MeinX509TrustManager.checkServerTrusted(TrustManagerFactory.java:35)

The error is in your trust manager code, as the stack trace clearly shows. It even tells you the line number.

The other problem here is the misleading error message you printed. There is no 'error in HTTPS connection here', just a simple NPE in your own code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top