Android OutputStreamWriter ' s flush method throws IOException

Source: Internet
Author: User
Tags http post

<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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.