【问题标题】:How to ensure data has been post (sent) to remote server如何确保数据已发布(发送)到远程服务器
【发布时间】:2013-01-03 14:55:05
【问题描述】:

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

    }

【问题讨论】:

    标签: android post get


    【解决方案1】:

    为每个帖子添加一个 GUID,并返回一个返回 GUID 以及交易哈希的响应。跟踪服务器上的 GUID。如果服务器的响应无效,请重试。如果收到具有相同 GUID 的事务,则将其丢弃,但确认已收到。

    【讨论】:

    • 感谢您的参与,很有用。
    【解决方案2】:

    我相信对于这两个问题,HTTP 200 响应代码可确保您的请求已成功发布到服务器中。

    【讨论】:

    猜你喜欢
    • 2013-06-05
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 2011-06-30
    • 2013-07-16
    • 1970-01-01
    相关资源
    最近更新 更多