標籤:android style blog http color io os ar java
============問題描述============
以下是android發送Http請求的代碼,但是經常出現:伺服器響應很久, android端出現ReadTimeout, android端又會再發一次同樣的請求,導致資料重複。有人說是jdk1.4.2的問題,但我用的是1.6.0。請大神們幫忙
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() == HttpURLConnection.HTTP_OK) // ResponseCode:200{InputStream inStream = conn.getInputStream();//int contentLen = conn.getContentLength();result = readInputStream(inStream,1024);inStream.close();} else {result = "500"; //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();}
============解決方案1============
嘗試用 httpclient 吧
看不出什麼問題,屬於疑難雜症吧
============解決方案2============
底層的實現中 如果發現http請求逾時 有重發機制
============解決方案3============
引用 16 樓 Roy_se7en 的回複:
Quote: 引用 15 樓 ysh512 的回複:
Quote: 引用 13 樓 Roy_se7en 的回複:
Quote: 引用 11 樓 ysh512 的回複:
底層的實現中 如果發現http請求逾時 有重發機制
請問,這個有資料可查嗎?如何能禁用呢?
以前在一個人的blog中看到的,現在找不到了
麻煩幫忙找下,萬分感謝。
原來那個找不到了 你看看這個 意思類似
http://blog.csdn.net/hanqunfeng/article/details/4510338
============解決方案4============
http://developer.nokia.com/community/wiki/Using_HTTP_and_HTTPS_in_Java_ME
這個你看下
還有你的j2me 中的相關代碼 也請po出來, 前面的是 android中的代碼。
============解決方案5============
1:
http://bugs.java.com/view_bug.do?bug_id=6672144
這裡面描述的 bug 和你說的很像。
它的解決方案是設定系統屬性。
From JDK6 you can set the system property, sun.net.http.retryPost, to false to prevent the HTTP client from silently resending 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
這個完全是你說的案例。它的建議是後台參與進來
============解決方案6============
引用 32 樓 Roy_se7en 的回複:
Quote: 引用 31 樓 foolsheep 的回複:
連線逾時和讀取逾時時間會不會不一致?
這2者不一致有關係嗎?
如果串連還沒有逾時,而讀取時間逾時的話,HttpUrlConnection就會再重新發送請求。所以,要解決這個重發問題,可以試一下,把讀取逾時的時間設定得比連線逾時稍長一點,因為有時候,jdk計算的時間會有點偏差,這樣可以保證在連線逾時之前不會讀取逾時
Http請求重複發送