求助!!!调用httpconnection.getInputStream方法总是出io异常,求大神解决!

南人旧梦_ 2014-04-25 11:29:43
代码如下,可以连接到服务器,而且响应吗也是200,但是只要一调用getInputstream(),就会报io异常,stream closed。
new Thread() {
public void run() {
try {
URL url = new URL(address);
try {

HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setConnectTimeout(3000);

int code = conn.getResponseCode();
System.out.println(code+"");
InputStream in = conn.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = -1;
byte[] buffer = new byte[1024];

while ((len = in.read(buffer)) != -1) {
baos.write(buffer, 0, len);
in.close();
baos.close();
}

String content = new String(baos.toByteArray());
System.out.print(content);
/*contentTV.setText(content);*/

} catch (IOException e) {
System.out.println("io异常");
e.printStackTrace();
}
} catch (MalformedURLException e) {
System.out.println("url异常");
e.printStackTrace();
}

}

}.start();


logcat截图如下:
...全文
939 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
南人旧梦_ 2014-04-25
  • 打赏
  • 举报
回复
引用
rtyou1234
我擦。。。我傻了,写错地方了。问题解决了,谢谢啦。
heavenfeng 2014-04-25
  • 打赏
  • 举报
回复
new Thread() { 多线程访问同一个流,当一个线程关闭了其他线程又访问该资源,
逍遥笑 2014-04-25
  • 打赏
  • 举报
回复
你怎么在while里面把流关闭了?数据都还没读完吧

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧