我有一个关于ssl
. 我连接到https
链接并从中获取json
。它运行良好,但有时无法连接并从中获取json
。结果是我的应用程序崩溃了。我认为它必须有一种方法来打开连接并从 https 获取 json 。
我在 LG Optimus G 中使用Android 4.1.2
.
这是我的错误日志:
10-24 04:10:59.149: W/System.err(9985): javax.net.ssl.SSLException: Connection closed by peer
10-24 04:10:59.149: W/System.err(9985): at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
10-24 04:10:59.169: W/System.err(9985): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:395)
10-24 04:10:59.169: W/System.err(9985): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.<init>(OpenSSLSocketImpl.java:647)
10-24 04:10:59.189: W/System.err(9985): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:618)
10-24 04:10:59.209: I/MiuiMagnifierController(9985): MagnifierController is created
10-24 04:10:59.219: W/System.err(9985): at org.apache.http.impl.io.SocketInputBuffer.<init>(SocketInputBuffer.java:70)
10-24 04:10:59.229: W/System.err(9985): at org.apache.http.impl.SocketHttpClientConnection.createSessionInputBuffer(SocketHttpClientConnection.java:83)
10-24 04:10:59.229: W/System.err(9985): at org.apache.http.impl.conn.DefaultClientConnection.createSessionInputBuffer(DefaultClientConnection.java:170)
10-24 04:10:59.229: W/System.err(9985): at org.apache.http.impl.SocketHttpClientConnection.bind(SocketHttpClientConnection.java:106)
10-24 04:10:59.229: W/System.err(9985): at org.apache.http.impl.conn.DefaultClientConnection.openCompleted(DefaultClientConnection.java:129)
10-24 04:10:59.229: W/System.err(9985): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:172)
10-24 04:10:59.239: W/System.err(9985): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
这是我使用的功能:
public static String readJsonFromUrl(String url) throws Exception {
String result = "";
HttpPost httpPost = null;
DefaultHttpClient httpClient = null;
httpClient = httpClient();
httpPost = new HttpPost(url);
HttpResponse httpResponseGet = httpClient.execute(httpPost);
HttpEntity resEntityGet = httpResponseGet.getEntity();
result = EntityUtils.toString(resEntityGet);
if (result.contains("\n")) {
result = result.replace("\n", "");
}
return result;
}
=> 它是在行中崩溃的:HttpResponse httpResponseGet = httpClient.execute(httpPost);
你能给我一些可以解决这个问题的解决方案吗?