码迷,mamicode.com
首页 > 移动开发 > 详细

Android OutputStreamWriter's flush method throws IOException

时间:2015-05-11 12:58:48      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:android

<span style="white-space: pre;">	Android开发弱网络客户端的时候,需要给服务器发送HTTP POST请求,首先进行设置</span>
<span style="white-space: pre;">	</span>URL url = new URL(urlString);
	connection = (HttpURLConnection) url.openConnection();
	connection.setConnectTimeout(Constants.CONNECTION_TIMEOUT);
	connection.setReadTimeout(Constants.IO_TIMEOUT);
	connection.setDoOutput(true);
	connection.setRequestMethod("POST");
	connection.setUseCaches(false);
	connection.setRequestProperty("Content-Type",
	<span style="white-space: pre;">	</span>"application/json:charset=utf-8");
	connection.setDoInput(true);
	connection.setDoOutput(true);
<span>	connection.connect();</span>
<span style="white-space: pre;"><span style="white-space: pre;">	</span>osw = new OutputStreamWriter(connection.getOutputStream());</span>
<span style="white-space:pre">	BufferedReader reader = new BufferedReader(
<span style="white-space:pre">		</span>new InputStreamReader(conn.getInputStream()));
</span><span style="white-space:pre">	</span>//本语句是造成IOException的原因  (1)

然后写入数据

	String requestContent = "[" + "{\"ipaddress\":\"" + localIp + "\","
						+ "\"nettype\":\"" + (netType) + "\"," + "\"envtype\":\""
						+ envType  + "\"}" + "]";
	osw.write(requestContent);
	osw.flush();
	osw.close();
报了IOException的错误,此时HTTP的写内容还没完毕,就在代码(1)处开了一个BufferReader,所以造成了IOException。

                还有其它人遇到同样的问题,但是原因不一样,请参考

http://stackoverflow.com/questions/4736042/outputstreamwriters-flush-method-throws-ioexception-when-trying-to-write-chines

Android OutputStreamWriter's flush method throws IOException

标签:android

原文地址:http://blog.csdn.net/mooreliu/article/details/45642747

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!