2 qq 16259457 qq_16259457 于 2016.09.07 10:14 提问

android 中使用HttpURLConnection报错

代码如下:

                private String HttpConnection(String urlString) throws IOException {
        HttpURLConnection urlConnection = null;
        BufferedInputStream in = null;
        final URL url;
        try {
            url = new URL(urlString);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setConnectTimeout(5000);
            in = new BufferedInputStream(urlConnection.getInputStream());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        InputStreamReader isr;
        String result="";
        String line="";
        try {
            if (in!=null){
            isr=new InputStreamReader(in,"utf-8");
            BufferedReader br=new BufferedReader(isr);
            while ((line=br.readLine())!=null){
                result+=line;
            }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        urlConnection.disconnect();
        return result;
    }

```使用移动网络就会报这个09-07 09:47:24.705: W/System.err(21672): java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)异常,然而如果用wifi网络就没有问题,把网址直接输入浏览器中打来也没有问题。这是怎么回事呢


1个回答

qq_27665781
qq_27665781   Rxr 2016.09.07 10:14

服务端是用的是内网吧。

qq_16259457
qq_16259457 不是,我是打开“都来读”这个小说网站的页面,换成了用okhttp请求也还是那样,wifi可以正常连接,手机本身的4G网络就不行,仍然是那样报错
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片