HttpURLConnection Parabolic Anomaly Java.io.FileNotFoundException

Source: Internet
Author: User

Today, when debugging the app, the httpurlconnection to get the server data with the java.io.FileNotFoundException exception, the reason is not clear, but has found a solution.


Because we have two types of server data, one is XML (request with parameter, that is, post method), one is JSON format (request parameter directly appended to the URL, should be a get way).

Both of these data were previously requested in the same way as the Post method, as follows:

JSON data address is http://72.52.93.xxx/hdepg_data/dataForQuanQiuBo/2220143.js

/** * Post mode request * @param param request parameter * @param URLSTR request address * @param contentType content Format * @return/Public Inpu
		Tstream postmethod (string param, String urlstr, String contentType) {InputStream is = null;
			try {byte[] XmlData = param.getbytes ("UTF-8");
			URL url = new URL (urlstr);
			URLConnection Urlcon = Url.openconnection ();
			Urlcon.setdooutput (FALSE);
			Urlcon.setdoinput (TRUE);
			Urlcon.setconnecttimeout (40000);
			Urlcon.setreadtimeout (40000);
			Urlcon.setusecaches (FALSE);
			Urlcon.setrequestproperty ("Content-type", ContentType);
			Urlcon.setrequestproperty ("Content-length", String.valueof (Xmldata.length));
			DataOutputStream printout = new DataOutputStream (Urlcon.getoutputstream ());
			Printout.write (XmlData);
			Printout.flush ();
			Printout.close ();
		is = Urlcon.getinputstream ();
			catch (IOException e) {log.e (TAG, "URL connection failed!");
		E.printstacktrace (); catch (Exception e) {log.e (TAG, "Failed getting input strEam.
			");
		E.printstacktrace ();
	return is; }

Then the JSON data changed to an address: http://xxx.hanyastar.com/hdepg_data/dataForQuanQiuBo/ Xxxx.js, and then the post way to request data when there is java.io.FileNotFoundException abnormal. Feel very strange, just changed the address, the other operations have not changed, why can not successfully visit it.


Later on the Internet to check, found that someone said:

Original address: Http://www.mikebai.com/html/2012-03/939.html has

recently brushed the machine to 4.0.
ran his app and found that the download XML file part throws an exception: Java.io.filenotfoundexception:http://www.jpfocus.com/xxxx.xml is

in the 2.3 system but can download

My code normally:

URL url = new URL (URLSTR);
HttpURLConnection Httpcon = (httpurlconnection) url.openconnection ();
Httpcon.setrequestmethod ("get");
Httpcon.setdooutput (true);
Httpcon.connect ();

Reason:
setting Httpcon.setdooutput (TRUE) in 4.0 will cause the request to be submitted as post, even if the Httpcon.setrequestmethod ("get") is set;
Httpcon.setdooutput (true) in code;



the explanation on Setdooutput (true)

is that setting true indicates that the request you sent will send the contents of the body to the server side , that is, post and put need to be used. Get is completely free of setup.

The new address does not support post mode.

So I modified the request method:

Public InputStream Postmethodhanya (string param, String urlstr, String contentType) {InputStream is = null;
			try {byte[] XmlData = param.getbytes ("UTF-8");
			URL url = new URL (urlstr);
			URLConnection Urlcon = Url.openconnection ();
			Urlcon.setdooutput (FALSE);
			Urlcon.setdoinput (TRUE);
			Urlcon.setconnecttimeout (40000);
			Urlcon.setreadtimeout (40000);
Urlcon.setusecaches (FALSE);
Urlcon.setrequestproperty ("Content-type", ContentType);
Urlcon.setrequestproperty ("Content-length",//String.valueof (xmldata.length));
Urlcon.setfixedlengthstreamingmode (xmldata.length);
DataOutputStream printout = new DataOutputStream (//Urlcon.getoutputstream ());
Printout.write (XmlData);
Printout.flush ();
			Printout.close ();
		is = Urlcon.getinputstream ();
			catch (IOException e) {log.e (TAG, "URL connection failed!");
		E.printstacktrace (); catch (Exception e) {log.e (TAG, "Failed getting input stream.)
			"); E.printstacktrace ();
	return is; }
Comment out the body parameter of post (if not commented, and setdooutput set to False will be reported: Java.net.ProtocolException:method does not support a request body: Get), Setdooutput is set to False (or commented out). Using this method to get the new address JSON data is successful.


Although the JSON data was obtained using GET, the reason why there was a difference between two addresses is still not clear, as recorded here. If a great God knows, please enlighten me.



The meaning of Ps:setdoinput and setdooutput

Public void setdoinput (boolean doinput) This  URLConnection   doInput  The value of the field is set to the specified value.      url  connections can be used for input and/or output. If you intend to use the  URL  connection for input, set the  DoInput  flag to  true; if it is not intended to be used, set to  false. The default value is  true.    Public void setdooutput (boolean dooutput) will this  URLConnection   dooutput The value of the   field is set to the specified value.      url  connections can be used for input and/or output. If you intend to use the  URL  connection for output, set the  DoOutput  flag to  true; if it is not intended to be used, set to  false. The default value is  false.    

Httpurlconnection.setdooutput (TRUE); You can use Conn.getoutputstream () later. Write ()    Httpurlconnection.setdoinput (True); Conn.getinputstream () can be used later. Read ();      The GET request is not conn.getoutputstream () because the parameter is appended directly to the address, so the default is false.    Post requests (for example: file uploads) require a large amount of data to be transmitted to the service area, which is placed in the HTTP body, so that data is written to the server after the connection is established.       The default value is true because Conn.getinputstream () is always used to get the response from the service side.  


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.