我正在尝试对URL进行GET,并且每次都看到以下一致且可重现的行为。
getResponseCode
获得IO异常。 (引起:android.system.ErrnoException:recvfrom失败:ECONNRESET(由对等方重置连接))。我试图将keep-alive设置为false(如另一个问题所示)并且没有帮助。
修改:发布我的主getJSon
方法的代码:
public static String getJSON(String url, int timeout) {
HttpURLConnection c = null;
try {
URL u = new URL(url);
System.setProperty("http.keepAlive", "false");
c = (HttpURLConnection) u.openConnection();
CookieManager cookieManager = CookieManager.getInstance();
String cookie = cookieManager.getCookie(u.getHost());
c.setRequestProperty("Cookie", cookie);
if(cookie == null || cookie.isEmpty()) {
Log.i(TAG,"No authentication cookie. Not trying to GET from the self api.");
return ""; //No authentication cookie. GET call will fail.
}
c.setRequestMethod("GET");
c.setRequestProperty("Content-length", "0");
c.setUseCaches(false);
c.setAllowUserInteraction(false);
c.setConnectTimeout(timeout);
c.setReadTimeout(timeout);
c.connect();
int status = c.getResponseCode();
Log.i(TAG,"Status Code in getJSON:" + status);
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
return sb.toString();
default:
Log.e(TAG,"Failure retrieving contents from URL:"+ url +" HTTP code:" + status);
return "";
}
} catch (MalformedURLException ex) {
Log.e(TAG,"Malformed URL:" + url);
} catch (IOException ex) {
Log.e(TAG,"IOException when retrieving user data from URL");
ex.printStackTrace();
} finally {
if (c != null) {
try {
c.disconnect();
} catch (Exception ex) {
Log.e(TAG,"Exception when disconnecting url resource when retrieving user data from URL.");
}
}
}
return "";
}
异常堆栈跟踪:
05-19 10:20:30.154 18528-20770/com.example W/System.err﹕ java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
05-19 10:20:30.165 18528-20770/com.example W/System.err﹕ at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:592)
05-19 10:20:30.165 18528-20770/com.example W/System.err﹕ at libcore.io.IoBridge.recvfrom(IoBridge.java:556)
05-19 10:20:30.165 18528-20770/com.example W/System.err﹕ at java.net.PlainSocketImpl.read(PlainSocketImpl.java:485)
05-19 10:20:30.165 18528-20770/com.example W/System.err﹕ at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
05-19 10:20:30.165 18528-20770/com.example W/System.err﹕ at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
05-19 10:20:30.165 18528-20770/com.example W/System.err﹕ at com.android.okio.Okio$2.read(Okio.java:113)
05-19 10:20:30.165 18528-20770/com.example W/System.err﹕ at com.android.okio.RealBufferedSource.indexOf(RealBufferedSource.java:147)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.android.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:94)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:175)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:616)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:379)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:491)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.example.ConnectionHelper.getJSON(ConnectionHelper.java:98)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.example.RegistrationHelper.retrieveAndStoreUserData(RegistrationHelper.java:296)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.example.RegistrationHelper.registerDeviceWithBackend(RegistrationHelper.java:444)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.example.RegistrationHelper.access$400(RegistrationHelper.java:31)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.example.RegistrationHelper$1.doInBackground(RegistrationHelper.java:197)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at com.eclinic247.www.eclinicdr.RegistrationHelper$1.doInBackground(RegistrationHelper.java:177)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
05-19 10:20:30.166 18528-20770/com.example W/System.err﹕ Caused by: android.system.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
05-19 10:20:30.167 18528-20770/com.example W/System.err﹕ at libcore.io.Posix.recvfromBytes(Native Method)
05-19 10:20:30.167 18528-20770/com.example W/System.err﹕ at libcore.io.Posix.recvfrom(Posix.java:161)
05-19 10:20:30.167 18528-20770/com.example W/System.err﹕ at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:250)
05-19 10:20:30.167 18528-20770/com.example W/System.err﹕ at libcore.io.IoBridge.recvfrom(IoBridge.java:553)
05-19 10:20:30.167 18528-20770/com.example W/System.err﹕ ... 24 more