我有API 15 + 16問題,17我不知道,但我知道在18和19它工作。Android HttpsURLConnection問題
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());
*
* }
*/
}
}
...
錯誤日誌:
04-25 23:05:17.562:E /主(21301):錯誤https連接顯示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.mdis co.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):在libcore.net.http.HttpConnection.setupSecureSocket(HttpConnection.java:209) 04-25 23:05:17.582: W/System.err(21301):在libcore.net.http.HttpsURLConnectionImpl $ HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:478) 04-25 23:05:17.582:W/System.err(21301):位於libcore。 net.http.HttpsURLConnectionImpl $ HttpsEngine.connect(HttpsURLConnectionImpl.java:433) 04-25 23:05:17.582:W/System.err(21301):在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):在libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80) 04-25 23:05:17.582:W/System.err(21301):位於libcore.net.http。 HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:165) 04-25 23:05:17.582:W/System.err(21301):在de.mdisco.HWRapp.Main $ MyThread.run(M ain.java:1204) 04-25 23:05:17.582:E/Main(21301):錯誤緩衝讀取結果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):在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):在libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289) 04-25 23:05:17.582:W/System.err(21301):位於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):在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):錯誤緩衝Reader結果java.lang.NullPointerException
因此,我認爲一個安全的SSL連接無法建立。
但是我的錯誤在代碼裏面呢?
感謝您的幫助!
檢查這一個http://stackoverflow.com/questions/7125275/android-https-ssl-connection-usign-httpsurlconnection?rq=1 – UrielUVD