Android HTTP URL连接从getResponseCode抛出IOException

时间:2015-05-18 07:20:50

标签: java android httpurlconnection ioexception

我正在尝试对URL进行GET,并且每次都看到以下一致且可重现的行为。

  1. 当我从我的运营商处获得LTE连接时,一切都运行良好,没有任何问题。
  2. 每当我在工作时切换到我的wifi连接时,我都会从getResponseCode获得IO异常。 (引起:android.system.ErrnoException:recvfrom失败:ECONNRESET(由对等方重置连接))。
  3. 工作中的Wi-Fi设置为通过防火墙和登录机制。所以,我认为这可能与它有关(即使登录会话有效约2天。因此,一旦从设备登录,应该没有任何问题)。但是,即使使用wi-fi工作,如果我只是尝试直接从设备上的浏览器获取URL内容,它会很好地检索页面(其余的api只返回一个非常小而简单的json格式化主体)。因此,这不是由于从设备到服务器的任何连接问题。
  4. 此外,在同一个应用程序中,我尝试使用webview而不是api调用加载该页面,并且加载得很好。
  5. 我试图将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
    

0 个答案:

没有答案