httprequest handle time delays till having response
Posted
by
bourax webmaster
on Stack Overflow
See other posts from Stack Overflow
or by bourax webmaster
Published on 2013-11-10T09:48:52Z
Indexed on
2013/11/10
9:53 UTC
Read the original article
Hit count: 438
I have an application that calls a function to send JSON object to a REST API, my problem is how can I handle time delays and repeat this function till i have a response from the server in case of interrupted network connexion ?? I try to use the Handler but i don't know how to stop it when i get a response !!!
here's my function that is called when button clicked :
protected void sendJson(final String name, final String email, final String homepage,final Long unixTime,final String bundleId) {
Thread t = new Thread() {
public void run() {
Looper.prepare(); //For Preparing Message Pool for the child Thread
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
HttpResponse response;
JSONObject json = new JSONObject();
//creating meta object
JSONObject metaJson = new JSONObject();
try {
HttpPost post = new HttpPost("http://util.trademob.com:5000/cards");
metaJson.put("time", unixTime);
metaJson.put("bundleId", bundleId);
json.put("name", name);
json.put("email", email);
json.put("homepage", homepage);
//add the meta in the root object
json.put("meta", metaJson);
StringEntity se = new StringEntity( json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
String authorizationString = "Basic " + Base64.encodeToString(
("tester" + ":" + "tm-sdktest").getBytes(),
Base64.NO_WRAP); //Base64.NO_WRAP flag
post.setHeader("Authorization", authorizationString);
response = client.execute(post);
String temp = EntityUtils.toString(response.getEntity());
Toast.makeText(getApplicationContext(), temp, Toast.LENGTH_LONG).show();
} catch(Exception e) {
e.printStackTrace();
}
Looper.loop(); //Loop in the message queue
}
};
t.start();
}
© Stack Overflow or respective owner