我在加载webview时检查状态代码错误,以检查是否有任何http错误。
虽然当我尝试检查我的设备是否可以连接到链接时https://www.google.pt/?gws_rd=ssl几乎总是会出现以下错误:
java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:545)
at libcore.io.IoBridge.recvfrom(IoBridge.java:509)
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:489)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:241)
at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
at org.apache.http.impl.client.DefaultRequestDirector.createTunnelToTarget(DefaultRequestDirector.java:765)
at org.apache.http.impl.client.DefaultRequestDirector.establishRoute(DefaultRequestDirector.java:664)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:384)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:575)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:498)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:476)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
at libcore.io.Posix.recvfromBytes(Native Method)
at libcore.io.Posix.recvfrom(Posix.java:141)
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
... 26 more
我尝试了其他链接,有时它也会出现此错误,很少会给我http 200。
以下是我执行请求的方式:
class RetrieveConnectionStatus extends AsyncTask<String, Integer, Integer> {
private Integer mHttpStatus;
protected Integer doInBackground(String... urls) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpRequest = new HttpGet(urls[0]);
HttpResponse httpResponse = httpClient.execute(httpRequest);
mHttpStatus = httpResponse.getStatusLine().getStatusCode();
// if( mHttpStatus != 200) {
} catch (Exception e) {
L.e(CampaignScreen.class, Log.getStackTraceString(e));
mHttpStatus = 404;
}
return mHttpStatus;
}
}
更新:我通过3G和http页面访问也会出现此错误。同样通过无线,这没有问题
有人可以帮我这个吗?到底是怎么回事?