============ Problem Description ============
Here is the code for Android to send HTTP requests, but often: the server responds for a long time, the Android side appears ReadTimeout, and the Android side sends the same request again, causing the data to repeat itself. Some say it's a jdk1.4.2 problem, but I'm using 1.6.0. Please help the gods!
URL url = null; httpurlconnection conn = null; string result = "";try {string sparam = "&m=" + methodName;if (Vector != null) {for (Int i = 0; i < vector.size () ; i++) {sParam += "&p" + string.valueof (i) + "=" + Vector.elementat (i). toString ();}} Byte[] data = sparam.getbytes (); Url = new url (Realurl.replace ("Xx.mobile", Methodname+ ". Mobile"));conn = (httpurlconnection) url.openconnection (); Conn.setconnecttimeout ( Connect_timeout); Conn.setreadtimeout (read_timeout); Conn.setdoinput (true); Conn.setdooutput (true); Conn.setrequestmethod (Method_post); Conn.setrequestproperty ("Connection", "keep-alive"); Conn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded"); Conn.setRequestProperty (" Content-length ", string.valueof (data.length)); Conn.setrequestproperty ("Charset", "Utf-8"); Conn.setusecaches (false); Outputstream outstream = conn.getoutputstream (); outstream.write (data); Outstream.flush (); Outstream.close ();if ( Conn.getresponsecode () &NBSP;==&NBSP;HTTPURLCONNECTION.HTTP_OK) // ResponseCode:200{InputStream Instream = conn.getinputstream ();//int contentlen = conn.getcontentlength (); result = readinputstream (instream,1024); Instream.close ();} else {result = "; //server error}} catch " (connecttimeoutexception e) {// todo: handle exceptioncommondata.log ("Connecttimeoutexception");throw New myexception (http_result_connect_timeout);} catch (sockettimeoutexception e) {// todo: handle exceptioncommondata.log ( "Sockettimeoutexception"); Throw new myexception (http_result_read_timeout);} catch (exception e) {// Todo: handle exceptioncommondata.log (E.getlocalizedmessage ()); Throw new myexception (HTTP_ result_exception);} finally {conn.disconnect ();}
============ Solution 1============
Try using HttpClient.
I don't see any problem, it belongs to the incurable diseases.
============ Solution 2============
In the underlying implementation, if you find that the HTTP request timed out has a re-send mechanism
============ Solution 3============
Reference 16 Floor Roy_se7en's reply:
Quote: Referring to the reply of the 15 floor ysh512:
Quote: Referring to the reply of the 13 floor Roy_se7en:
Quote: Referring to the reply of the 11 floor ysh512:
In the underlying implementation, if you find that the HTTP request timed out has a re-send mechanism
Excuse me, do you have any information to check this? How can I disable it?
I've seen it in one's blog, and I can't find it now.
Please help to find, thank you very much.
That's not what you're looking at.
http://blog.csdn.net/hanqunfeng/article/details/4510338
============ Solution 4============
Http://developer.nokia.com/community/wiki/Using_HTTP_and_HTTPS_in_Java_ME
You look at this.
Also the relevant code in your J2ME also please po out, the front is the code in Android.
============ Solution 5============
1:
http://bugs.java.com/view_bug.do?bug_id=6672144
The bug described here is very similar to what you said.
Its solution is to set system properties.
From JDK6 You can set the system property, Sun.net.http.retryPost, to False to prevent the HTTP client from silently R Esending the POST request.
Java-dsun.net.http.retrypost=false ...
2:
Http://developer.nokia.com/community/discussion/showthread.php/233689-HTTP-Connection-sending-request-twice-after-30-sec
This is exactly the case you're talking about. It's a suggestion to get involved in the background
============ Solution 6============
Reference 32 Floor Roy_se7en's reply:
Quote: Quote 31 floor Foolsheep reply:
Will the connection time-out and read timeout be inconsistent?
Are there any discrepancies between the 2?
If the connection has not timed out and the read time has timed out, HttpURLConnection will resend the request. So, to solve this problem, you can try to set the read time-out to a bit longer than the connection timeout, because sometimes the JDK calculates a bit of a bias, which guarantees that the timeout will not be read before the connection times out
HTTP request Repeat Send