gpt4 book ai didi

android - IOException BufferedInputStream 在 HTC 中关闭

转载 作者:行者123 更新时间:2023-11-29 22:08:39 26 4
gpt4 key购买 nike

我在执行 BufferedReader.readLine() 时遇到了 IOException,在下面完整代码的行 while ((line = br.readLine()) != null) {) 中。 Exception.getMessage() 返回 BufferedInputStream is closed

它只发生在 HTC 设备上,当我使用 Sony Ericsson 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 方法?

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

最佳答案

试一试 -

public static String getContent(String url) throws Exception {
return(new Scanner(new URL(url).openConnection().getInputStream()).useDelimiter("/z").next());
}

关于android - IOException BufferedInputStream 在 HTC 中关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10169705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com