簡體   English   中英

如何確保數據已發布(發送)到遠程服務器

[英]How to ensure data has been post (sent) to remote server

我正在使用此代碼通過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;

    }

在每個帖子中添加一個GUID,然后返回一個響應,返回GUID和事務的哈希值。 跟蹤服務器上的GUID。 如果服務器的響應無效,請重試。 如果收到具有相同GUID的事務,則將其丟棄,但確認已收到。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM