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