1

while ((line = br.readLine()) != null) {当执行下面完整代码的BufferedReader.readLine() 时,我得到了一个 IOException 。Exception.getMessage()返回的BufferedInputStream 是关闭的。

它只发生在 HTC 设备中,当我使用索尼爱立信 XPERIA 时它不会发生。

我的完整代码:

public static String downloadString(String url) throws MalformedURLException, IOException {     
    InputStream is = downloadStream(url);

    //Convert stream into String
    String line;
    BufferedReader br = new BufferedReader(new InputStreamReader(is), 4096);            
    StringBuilder sb =  new StringBuilder();
    while ((line = br.readLine()) != null) {
        sb.append(line);
    }
    br.close();
    return sb.toString();
}

public static InputStream downloadStream(String url) throws MalformedURLException, IOException {
    return connection(url).getInputStream();
}

private static HttpURLConnection connection(String s_url) throws MalformedURLException, IOException {
    URL url = new URL(s_url);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoOutput(true);
    urlConnection.connect();

    return urlConnection;
}

如何制作适用于每台设备的更好的 downloadString 方法?

提前致谢。非常感谢您的回答

4

2 回答 2

1

尝试一下 -

public static String getContent(String url) throws Exception {
      return(new Scanner(new URL(url).openConnection().getInputStream()).useDelimiter("/z").next());
}
于 2012-04-16T06:50:17.670 回答
1

尝试使用 HttpGet 处理 http GET 连接(和 HttpPost 处理 http POST)

并确保在不需要时始终关闭流。

这里使用 httpget 从服务器获取字符串的简单代码:

 private void executeRequest(HttpUriRequest request)
{
    HttpClient client = new DefaultHttpClient();

    HttpResponse httpResponse;
    try {
        httpResponse = client.execute(request);
        responseCode = httpResponse.getStatusLine().getStatusCode();
        message = httpResponse.getStatusLine().getReasonPhrase();

        System.out.println(responseCode + ":" +message);

        HttpEntity entity = httpResponse.getEntity();

        if (entity != null) {

            InputStream instream = entity.getContent();
            response = convertStreamToString(instream);

            // Closing the input stream will trigger connection release
            instream.close();
        }

    } catch (ClientProtocolException e)  {
        client.getConnectionManager().shutdown();
        Toast.makeText(null, "Error", Toast.LENGTH_LONG);
    } catch (IOException e) {
        client.getConnectionManager().shutdown();
        Toast.makeText(null, "Error", Toast.LENGTH_LONG);
    } catch (OAuthMessageSignerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (OAuthExpectationFailedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (OAuthCommunicationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private static String convertStreamToString(InputStream is) {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

你可以这样调用函数:

HttpGet request = new HttpGet(url);

执行请求(请求);

于 2012-04-16T09:19:02.490 回答