android - Re-run HTTP request as part of AsyncTask -


(updated clarity) might need re-run http request in android asynctask based on result back. i've pasted simple example of asynchronous task below. instead of getting error code, getting temporary working-id tells me server processing request. need check every few seconds , send server working-id until finishes , gives me data. that's it! sounds pretty simple can't seem figure out:

private class httpgetter extends asynctask<url, void, void> {      @override     protected void doinbackground(url... urls) {         // todo auto-generated method stub         stringbuilder builder = new stringbuilder();         httpclient client = new defaulthttpclient();         httpget httpget = new httpget(urls[0]);          try {             httpresponse response = client.execute(httpget);             statusline statusline = response.getstatusline();             int statuscode = statusline.getstatuscode();              if (statuscode == 200) {                 httpentity entity = response.getentity();                 inputstream content = entity.getcontent();                 bufferedreader reader = new bufferedreader(new inputstreamreader(content));                 string line;                 while ((line = reader.readline()) != null) {                     builder.append(line);                 }                 log.v("getter", "your data: " + builder.tostring()); // response                                                                      // data             } else {                 log.e("getter", "failed download file");             }         } catch (clientprotocolexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }          return null;     } } 

then using asynctask:

httpgetter = new httpgetter(); get.execute("http://www.google.es"); 

here improved version of getter asynctask fetches "working id" , uses poll httpd until comes back.
need modify @ least parts marked todo comments match needs.

private class httpgetter extends asynctask<string, void, void> {      private string parseworkingid(string responsedata) {         // todo: implement me: parse json/xml/whatever         return responsedata;     }      private string getworkingid(httpclient client, string url) throws ioexception {         stringbuilder builder = new stringbuilder();         httpget httpget = new httpget(url);          httpresponse response = client.execute(httpget);         statusline statusline = response.getstatusline();         int statuscode = statusline.getstatuscode();          if (statuscode == 200) {             httpentity entity = response.getentity();             inputstream content = entity.getcontent();             bufferedreader reader = new bufferedreader(new inputstreamreader(content));             string line;             while ((line = reader.readline()) != null) {                 // don't forget add stripped line ending                 builder.append(line).append("\n");             }             string responsedata = builder.tostring();             log.v("getter", "your data: " + responsedata);             return parseworkingid(responsedata);         } else {             log.e("getter", "failed download file");             throw new ioexception("download failed");         }     }      private string getdata(httpclient client, string url, string workingid) throws ioexception {         // todo result data string? maybe want use stream         stringbuilder builder = new stringbuilder();         // todo change me matching needs         httpget httpget = new httpget(url + "?workingid=" + uri.encode(workingid));          httpresponse response = client.execute(httpget);         statusline statusline = response.getstatusline();         int statuscode = statusline.getstatuscode();          if (statuscode == 200) {             httpentity entity = response.getentity();             inputstream content = entity.getcontent();             bufferedreader reader = new bufferedreader(new inputstreamreader(content));             string line;             while ((line = reader.readline()) != null) {                 // don't forget add stripped line ending                 builder.append(line).append("\n");             }             string responsedata = builder.tostring();             if (responsedata.length() > 0) {                 log.v("getter", "your data: " + responsedata);                 return responsedata;             } else {                 log.v("getter", "nothing here yet");                 return null;             }         } else {             log.e("getter", "failed download file");             return null;         }     }      @override     protected void doinbackground(string... urls) {         string url = urls[0];         httpclient client = new defaulthttpclient();         string data = null;          try {             // working id             string workingid = getworkingid(client, url);              // loop getting data             while (true) { // todo add timeout or other sane break condition                 data = getdata(client, url, workingid);                 if (data == null) {                     try {                         thread.sleep(10 * 1000); // 10s                     } catch (interruptedexception e) {                         e.printstacktrace();                     }                 } else {                     break;                 }             }         } catch (clientprotocolexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }          if (data != null) {             // todo data             log.v("getter", "here data: " + data);         }          // maybe want return data onpostexecute()         return null;     } } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -