Retry Http(s) Posts Until They Succeed On Android
I have some data which will be sent from an Android app to a server via http(s). It needs to be sent in-order. Does there already exist a way of queuing http requests (for the sam
Solution 1:
There are several options to do it:
Volley:
RequestQueue queue = Volley.newRequestQueue(ctx); // ctx is the contextStringRequest req = newStringRequest(Request.Method.GET, url,
newResponse.Listener<String>() {
@OverridepublicvoidonResponse(String data) {
// We handle the response
}
},
newResponse.ErrorListener() {
@Override// handle response
}
);
queue.add(req);
OkHttp:
OkHttpClientclient=newOkHttpClient();
client.newCall(request).enqueue(newCallback() {
@OverridepublicvoidonFailure(Request request, IOException e) {
// Handle error
}
@OverridepublicvoidonResponse(Response response)throws IOException {
//handle response
}
});
Or you can use a counter in your requests and let the server order them. If you are interested to have more details about Android Http libraries i wrote a post recentely. Give a look here
Solution 2:
I am using this method for post https I am successfully done for all conditions.
privateclassAysncTaskextendsAsyncTask<Void,Void,Void>
{
privateProgressDialog regDialog=null;
@OverrideprotectedvoidonPreExecute()
{
super.onPreExecute();
regDialog=newProgressDialog(this);
regDialog.setTitle(getResources().getString(R.string.app_name));
regDialog.setMessage(getResources().getString(R.string.app_pleasewait));
regDialog.setIndeterminate(true);
regDialog.setCancelable(true);
regDialog.show();
}
@OverrideprotectedVoiddoInBackground(Void... params) {
try
{
newThread(newRunnable() {
@Overridepublicvoidrun() {
ArrayList<NameValuePair> postParameters = newArrayList<NameValuePair>();
postParameters.add(newBasicNameValuePair("param1",
paramvalue));
postParameters.add(newBasicNameValuePair("param2",
paramvalue));
String response = null;
try {
response = SimpleHttpClient
.executeHttpPost("url.php",
postParameters);
res = response.toString();
return res;
} catch (Exception e) {
e.printStackTrace();
errorMsg = e.getMessage();
}
}
}).start();
try {
Thread.sleep(3000);
// error.setText(resp);if (null != errorMsg && !errorMsg.isEmpty()) {
}
} catch (Exception e) {
}
}
catch (Exception e)
{
e.printStackTrace();
}
returnnull;
}
@OverrideprotectedvoidonPostExecute(Void result)
{
super.onPostExecute(result);
if(regDialog!=null)
{
regDialog.dismiss();
//do you code here you want
}
// do what u do
}
SimpleHttpClient.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
publicclassSimpleHttpClient {
/** The time it takes for our client to timeout */publicstaticfinalintHTTP_TIMEOUT=30 * 1000; // milliseconds/** Single instance of our HttpClient */privatestatic HttpClient mHttpClient;
/**
* Get our single instance of our HttpClient object.
*
* @return an HttpClient object with connection parameters set
*/privatestatic HttpClient getHttpClient() {
if (mHttpClient == null) {
mHttpClient = newDefaultHttpClient();
finalHttpParamsparams= mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
}
return mHttpClient;
}
publicstatic String executeHttpPost(String url, ArrayList<NameValuePair> postParameters)throws Exception {
BufferedReaderin=null;
try {
HttpClientclient= getHttpClient();
HttpPostrequest=newHttpPost(url);
UrlEncodedFormEntityformEntity=newUrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponseresponse= client.execute(request);
in = newBufferedReader(newInputStreamReader(response.getEntity().getContent()));
StringBuffersb=newStringBuffer("");
Stringline="";
StringNL= System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
Stringresult= sb.toString();
return result;
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
publicstatic String executeHttpatch(String url, ArrayList<NameValuePair> postParameters)throws Exception {
BufferedReaderin=null;
try {
HttpClientclient= getHttpClient();
HttpPostrequest=newHttpPost(url);
UrlEncodedFormEntityformEntity=newUrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponseresponse= client.execute(request);
in = newBufferedReader(newInputStreamReader(response.getEntity().getContent()));
StringBuffersb=newStringBuffer("");
Stringline="";
StringNL= System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
Stringresult= sb.toString();
return result;
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Performs an HTTP GET request to the specified url.
*
* @param url The web address to post the request to
* @return The result of the request
* @throws Exception
*/publicstatic String executeHttpGet(String url)throws Exception {
BufferedReaderin=null;
try {
HttpClientclient= getHttpClient();
HttpGetrequest=newHttpGet();
request.setURI(newURI(url));
HttpResponseresponse= client.execute(request);
in = newBufferedReader(newInputStreamReader(response.getEntity().getContent()));
StringBuffersb=newStringBuffer("");
Stringline="";
StringNL= System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
Stringresult= sb.toString();
return result;
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Post a Comment for "Retry Http(s) Posts Until They Succeed On Android"