android - How can i Handle special kind of exception in doinBackground() method -


i making android app requires fetch information remote server , therefore have make http request in async task.now problem that response take more 2 secs , when give http timeout exception of time works fine .so want implement functionality when recieve http timeout exception want retry request again(try doinbackground again,because network call can made in thread other main thread) because chances successful , things need fetched remote server occur in callremoteserver() method

now in program have implemented

new asynctask<void, void, void>() { private boolean httpresponseok = true;              @override             protected void doinbackground(void... params) {                 try {                  callremoteserver();                 }                  } catch (exception e) {                     httpresponseok = false;                     e.printstacktrace();                 }                  return null;             }              @override             protected void onpostexecute(void result) {                  if (httpresponseok == false) {          //show alert dialog stating unable coonect                                         } else {      //update ui information fetched }                                     });  

can advice me how can implement have mentioned above ,i mean if other exception other timeout show alert dialog otherwise retry atleast 5 time more callremoteserver method before showing dialog unable connect.

i not able think of way implement logic.

thanks in advance

you're getting connecttimeoutexception (or check in logs ioexception you're getting). first try extend timeout. similar answers can found here or here.

however, auto-reconnect mechanism must have. implement using recursive code:

final int maxattempts = 5; protected myserverdata callremoteserver(int attempt) throws ioexception {     try {         // io stuff , in case of success return data     } catch (connecttimeoutexception ex) {         if(attempt == maxattempts) {             return callremoteserver(attempt + 1);         } else {             throw ex;         }     } } 

your doinbackground method should like:

@override protected void doinbackground(void... params) {     try {          callremoteserver(0);     }      catch (exception e) {         e.printstacktrace();     }      return null;  } 

in way if connection timeouts attempt retry 5 max times (you can set max attempts like). make sure return data io operation valuable asset method anyway ...

for reason change following:

private class myasyncktask extends asynctask<void, void, myserverdata> {      @override     protected myserverdata doinbackground(void... params) {         try {              return callremoteserver(0);         }          catch (exception e) {             e.printstacktrace();         }          return null;      }      @override     protected void onpostexecute(myserverdata result) {         if(result != null) {             // display data on ui         }     } } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -