I’m having this code to post data to server by android application.
So , suppose before posting the data to server , the connection has lost (out of wifi or 3g)
or during post the connection lost.
How can I ensure the data has been post to server?
I implement a response from server side as a feedback message or as response, but do you think that is enough ?
Another scenario ( in critical system like a bank )
Suppose I send the data and it has posted well (e.g inserted to database ) but the error occur during getting the response !
Logically , because the response didn’t received , i will inform the user for example that the process didn’t posted and has to post it again! This is a problem !!
So what is the best way to implement or gain this level of insurance ?
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;
}
I believe for both the questions, HTTP 200 response code ensures that your requests are been posted successfully in the server.