我在Android應用程序,它發送數據到我的服務器(到HTTPS URL)的一段代碼爲這樣:ASN.1Exception Android中而將數據發送到服務器
private void sendData(String serverUrl, byte[] message) {
HttpURLConnection conn = null;
try {
URL url = new URL(serverUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setFixedLengthStreamingMode(message.length);
conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");
OutputStream os = new BufferedOutputStream(conn.getOutputStream());
os.write(message);
os.flush();
conn.connect();
} catch (Exception e) {
//TODO: log exception and continue gracefully
} finally {
if (conn != null)
conn.disconnect();
}
}
然而,我一直看到發生不時偶爾在某些設備上(通常與三星糖豆版本[SDK 16,17或18)以下異常:
org.apache.harmony.security.asn1.ASN1Exception: Wrong content for ASN.1 integer at [15].
An integer MUST be encoded in minimum number of octets
java.lang.RuntimeException: org.apache.harmony.security.asn1.ASN1Exception:
Wrong content for ASN.1 integer at [15]. An integer MUST be encoded in minimum number of octets
at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:586)
at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:371)
at libcore.net.http.HttpConnection.setupSecureSocket(HttpConnection.java:209)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:478)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:281)
at com.myapp.Reporter.sendData(Reporter.java:45)
我在網上搜索和谷歌高,低,但不能弄清楚這意味着什麼或如何解決它。
任何人有任何想法?
我會建議你使用凌雲庫進行所有的網絡通話。它易於使用和簡單.http://developer.android.com/training/volley/index.html – Arshad
謝謝。但是,我的代碼作爲第三方駐留在應用程序中,我不能假定它使用抽象,我不想將它作爲依賴項添加。 –