Andriod connecting WCF, HttpURLConnection filenotfoundexception

Source: Internet
Author: User

https://stackoverflow.com/questions/17991347/java-eofexception-when-getinputstream-from-post/18151239#18151239

If You use

conn.getInputStream()

Everytime, it would throw a in the case when java.io.FileNotFoundException your request was unsuccessful, basically for any HTTP response code of 40 0 or above. Your response body lies in

conn.getErrorStream()

Thus, you had to check the HTTP response code before you decide which stream to read from:

IntStatus=Conn.Getresponsecode();BufferedinputstreamInch;If (Status>= 400 ) { in = new  bufferedinputstream ( conn geterrorstream ()  ); } else {= new  Bufferedinputstream ( Conn. Getinputstream );                /span>                
BufferedReader reader = new BufferedReader (new InputStreamReader (in));
StringBuilder sb = new StringBuilder ();
String str;
while ((str = reader.readline ()) = null) {
Sb.append (str);
}

So you can see the error message.

Or, look at the error message on the server side of WCF errors.

<system.diagnostics>
<sources>
<source name= "System.ServiceModel" switchvalue= "Warning" propagateactivity= "true" >
<listeners>
<add name= "xml"/>
</listeners>
</source>
</sources>
<sharedListeners>
<add name= "xml" type= "System.Diagnostics.XmlWriterTraceListener" initializedata= "D:\wcf.svclog"/>
</sharedListeners>
</system.diagnostics>

--------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------

The meaning of Setdoinput and setdooutput

    1. public void setdoinput ( boolean doinput) sets the value of this  URLConnection   doInput  field to the specified value.     
    2. url  connections are available for input and/or output. If you intend to use the  URL  connection for input, set the  DoInput  flag to  true, or set to  false. The default value is  true.   
    3. public  Void setdooutput (boolean dooutput) this  URLConnection  's  dooutput The value of the   field is set to the specified value.     
    4. url  connections are available for input and/or output. If you intend to use the  URL  connection for output, set the  DoOutput  flag to  true, or set to  < if you do not intend to use it span class= "keyword" >false. The default value is  false.    
    1. Httpurlconnection.setdooutput (true); You can use Conn.getoutputstream () later. Write ()
    2. Httpurlconnection.setdoinput (true); You can use Conn.getinputstream later (). read ();
    3. The GET request does not use Conn.getoutputstream () because the parameter is appended directly to the address, so the default is false.
    4. Post requests (for example, file uploads) require a large amount of data to be transferred to the service area, which is placed inside the body of the HTTP, so you need to write the data to the server after the connection is established.
    5. Because Conn.getinputstream () is always used to get the response from the server, the default value is true.

Andriod connecting WCF, HttpURLConnection filenotfoundexception

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.