<span style= "WHITE-SPACE:PRE;" >android when developing a weak network client, you need to send an HTTP POST request to the server, first set </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>//This statement is the cause of IOException (1)
Then write the data
String requestcontent = "[" + "{\" ipaddress\ ": \" "+ Localip +" \ "," + "\" nettype\ ": \" "+ (NetType) +" \ "," + "\" envtype\ ": \ "+ envtype +" \ "}" + "]"; Osw.write (requestcontent); Osw.flush (); Osw.close ();
Reported IOException error, at this time the HTTP write content is not finished, in the Code (1) opened a bufferreader, so caused the ioexception.
There are other people who have the same problem, but for different reasons, please refer to
Http://stackoverflow.com/questions/4736042/outputstreamwriters-flush-method-throws-ioexception-when-trying-to-write-chines
Android OutputStreamWriter ' s flush method throws IOException