Question

I am making a call to server from my android app. I have done this using AsyncTask Thread. Now I have started Progress Dialog in onPreExecute() Method as follows:

protected void onPreExecute(){ 
dialog = new ProgressDialog(appcontext);
dialog.setMessage("Sending Position...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}

Now, in doInBackground() method I have been doing my sending data tasks.And after I get the response I am going to onPostExecute() method where I have used dialog.dismiss() to stop ProgressDialog.

my doInBackGround() code

protected String doInBackground(Position... position){
    InputStream inputStream = null;
    String temp=null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.2.2:8084/WebApplication5/getData");
String json = "";
JSONObject jsonObject = new JSONObject();
try { 
     jsonObject.accumulate("time",position[0].getTime()); 
     json = jsonObject.toString();
     StringEntity se = new StringEntity(json);
     httpPost.setEntity(se);
     httpPost.setHeader("Accept", "application/json");
     httpPost.setHeader("Content", "application/json");
     org.apache.http.HttpResponse httpResponse = httpclient.execute(httpPost);

  **// CODE ACTUALLY BLOCKS HERE UNTIL I GET A RESPONSE.(WHAT I THINKS). THOUGH WORKS 99%** 

     inputStream = httpResponse.getEntity().getContent();
     if(inputStream!=null)
          temp="sent"; 
 }
 catch (JSONException e){
     e.printStackTrace();
 }
 catch (IllegalStateException e) {
     e.printStackTrace();
     } 
 catch (IOException e) {
     e.printStackTrace();
     }
    return temp;
}

ALL IS FINE but sometimes I do not get a response (let it be any server error), In that case the dialog is unable to dismissed and is running undefinitely. I want to limit the duration of doInBackGround() method for few seconds. So that even in case of no response it will move to onPostExecute() and my infinte going dialog should stop.

Secondly, In case I get a response I want to save it to my SharedPreferences, so my question is what is the best way to do that, i.e.

1) I should do it in onPause() method.

2) or, in onStop() method,

3) or in onPostExecut()e method

or there is no point in that, I can do it anywhere.

And my last question is what is the what is the lifetime of data that is stored in Shared Preferences.

Was it helpful?

Solution

You can save it where you want, you could save it after you get it or in onPause (which is called when your activity is paused)

Save it in onPause if the user is going to modify it and you need to save it.

Or, if you just need it and you will not change it anymore save it in onPostExecute, or everytime you change it.

SharedPreference don't have a "lifetime" it will exists until the application is in the device, or until the user don't clear data app.

For your doBackground you could add a timeout to your request.

Change your code block:

HttpClient httpclient = new DefaultHttpClient();

to

BasicHttpParams params = new BasicHttpParams();
ConnManagerParams.setTimeout(params, TIMEOUT_IN_MILLISECONDS); // example: 2000 for 2 seconds
HttpClient httpClient = new DefaultHttpClient(params);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top