2013-01-03 51 views
0

我有這段代碼通過android應用程序發佈數據到服務器。 因此,假設在將數據發佈到服務器之前,連接丟失(超出wifi或3G) 或丟失連接期間。 如何確保數據已發佈到服務器? 我將服務器端的響應作爲反饋消息或作爲響應來實現,但您認爲這足夠嗎?如何確保數據已發佈(發送)到遠程服務器

另一種情況(像銀行關鍵的系統)

假設我發送數據,並將其張貼好(如插入到數據庫中),但得到的響應期間可能發生的錯誤! 從邏輯上說,因爲沒有收到響應,我會通知用戶例如該過程沒有發佈並且必須重新發布!這是個問題 !!

那麼實施或獲得此級別保險的最佳方法是什麼?

try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(http://www.exampleof.the/page_that_wil_receive_the_data.php?data=example); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent();   

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     //"ISO-8859-1,utf-8;q=0.7,*;q=0.7" 
     //"iso-8859-1" 
     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "utf-8"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // return JSON String 
     return jObj; 

    } 

回答

1

我相信對於這兩個問題,HTTP 200響應代碼可確保您的請求已成功發佈到服務器中。

+0

謝謝你,在這個主題更多http://stackoverflow.com/questions/6118768/posting-a-json-to-a-restful-webservice-getting-the-http-200-ok-but-it-returns –

+0

請勿在您的帖子中使用簽名/標語。您的用戶箱計爲您的簽名,您可以使用您的個人資料發佈您喜歡的任何關於您自己的信息。 [關於簽名/標語的常見問題](http://stackoverflow.com/faq#signatures) –

2

向每個帖子添加一個GUID,並返回一個返回GUID以及該事務的散列的響應。跟蹤服務器上的GUID。如果服務器的響應無效,請重試。如果收到具有相同GUID的交易,請將其丟棄,但確認已收到。

+0

感謝您的參與,有用。 –

相關問題