java - Android: How to send http request via service every 5 seconds -


i want request json data webservices after every 5 seconds. don't want show user i'm doing stuff in service. there no error in code not showing output in log expected.

here code:

protected void onhandleintent(intent intent) {      final string driverid = intent.getstringextra("driverid");      new handler().postdelayed(new runnable() {         public void run() {             httphandler sh = new httphandler();              // making request url , getting respose             string jsonstr = sh.makeservicecall(url + "?cno=" + driverid + "&lat=0&lon=79");              log.e("servicesclass", "response url: " + jsonstr);              if (jsonstr != null) {                 string temp = jsonstr;                 string finals = temp.replace("<string xmlns=\"http://tempuri.org/\">", "");                 log.e(tag, "response url @ service class: " + jsonstr);              } else {                 log.e(tag, "response url @ service class: " + jsonstr);             }         }     }, 5000); } 

code makeservicecall:

public string makeservicecall(string requrl){          string response = null;          try {              url url = new url(requrl);             httpurlconnection conn = (httpurlconnection) url.openconnection();             conn.setrequestmethod("get");               //read response             inputstream in = new bufferedinputstream(conn.getinputstream());             response = convertstreamtostring(in);         }catch (malformedurlexception e){             log.e(tag, "malformedexception: " +e.getmessage());         }catch (protocolexception e){             log.e(tag, "protocal exception: " +e.getmessage());         }catch (ioexception e){             log.e(tag, "ioexception: " +e.getmessage());         }catch (exception e){             log.e(tag, "exception: " +e.getmessage());         }         return response;     }      private string convertstreamtostring(inputstream is){          bufferedreader reader = new bufferedreader(new inputstreamreader(is));         stringbuilder sb = new stringbuilder();          string line;         try {             while ((line = reader.readline()) != null){                  sb.append(line).append('\n');             }         }catch (ioexception e){             e.printstacktrace();         }finally {             try {                 is.close();             }catch (ioexception e){                 e.printstacktrace();             }         }         return sb.tostring();     } 

can figure out code can error?

using alarm manager set repeating alarm.

alarmmanager alarmmanager = (alarmmanager) getsystemservice(context.alarm_service);     intent myintent = new intent(this, repeatingservice.class);     pendingintent pendingintent = pendingintent.getservice(this, 0, myintent, pendingintent.flag_update_current);     alarmmanager.setinexactrepeating(alarmmanager.elapsed_realtime, 5000, 5000, pendingintent); 

repeatingservice use intentservice because when job finished stopped automatically.

class repeatingservice extends intentservice{   override fun onhandleintent(intent: intent?) {    // stuff async or anyother task. }  } 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -