android - 12 requests are being fired to the server instead of once every 5 seconds in the AsyncTask class -


i sending simple json string {"tesla": "48.846305"} every 5 seconds server noticed many requests 12 being fired server in 1 second want send data once every 5 seconds. how can fix that?

i appreciate help.

output:

06-14 12:01:29.643: i/system.out(7915): output of : doinbackground {"tesla":23.91065752874712} 06-14 12:01:29.824: i/system.out(7915): output of stringbulder: {"status":280} 06-14 12:01:29.834: i/system.out(7915): output of : doinbackground {"tesla":23.91065752874712} 06-14 12:01:29.984: i/system.out(7915): output of stringbulder: {"status":280} 06-14 12:01:29.984: i/system.out(7915): output of : doinbackground {"tesla":23.91065752874712} 06-14 12:01:30.134: i/system.out(7915): output of stringbulder: {"status":280} 06-14 12:01:30.144: i/system.out(7915): output of : doinbackground {"tesla":23.91065752874712} 06-14 12:01:30.284: i/system.out(7915): output of stringbulder: {"status":280} 06-14 12:01:30.284: i/system.out(7915): output of : doinbackground {"tesla":23.91065752874712} 06-14 12:01:30.454: i/system.out(7915): output of stringbulder: {"status":280} 06-14 12:01:30.454: i/system.out(7915): output of : doinbackground {"tesla":23.91065752874712} 06-14 12:01:30.604: i/system.out(7915): output of stringbulder: {"status":280} 06-14 12:01:30.604: i/system.out(7915): output of : doinbackground {"tesla":23.91065752874712} 06-14 12:01:30.755: i/system.out(7915): output of stringbulder: {"status":280} 06-14 12:01:30.755: i/system.out(7915): output of : doinbackground {"tesla":23.91065752874712} 06-14 12:01:30.895: i/system.out(7915): output of stringbulder: {"status":280} 06-14 12:01:30.905: i/system.out(7915): output of : doinbackground {"tesla":23.91065752874712} 06-14 12:01:31.085: i/system.out(7915): output of stringbulder: {"status":280} 06-14 12:01:31.085: i/system.out(7915): output of : doinbackground {"tesla":23.776338448540205} 06-14 12:01:31.225: i/system.out(7915): output of stringbulder: {"status":280} 06-14 12:01:31.225: i/system.out(7915): output of : doinbackground {"tesla":23.776338448540205} 06-14 12:01:31.365: i/system.out(7915): output of stringbulder: {"status":280} 06-14 12:01:31.365: i/system.out(7915): output of : doinbackground {"tesla":23.776338448540205} 06-14 12:01:31.525: i/system.out(7915): output of stringbulder: {"status":280} 06-14 12:01:31.525: i/system.out(7915): output of : doinbackground {"tesla":23.776338448540205} 06-14 12:01:31.705: i/system.out(7915): output of stringbulder: {"status":280} 06-14 12:01:31.705: i/system.out(7915): output of : doinbackground {"tesla":23.776338448540205} 06-14 12:01:31.856: i/system.out(7915): output of stringbulder: {"status":280} 06-14 12:01:31.856: i/system.out(7915): output of : doinbackground {"tesla":23.776338448540205} 06-14 12:01:32.046: i/system.out(7915): output of stringbulder: {"status":280} 

code:

public void onsensorchanged(sensorevent sensorevent) {         synchronized (this) {             if (sensorevent.sensor.gettype() == sensor.type_magnetic_field) {                 float magx = sensorevent.values[0];                 float magy = sensorevent.values[1];                 float magz = sensorevent.values[2];                  magneticx.settext(float.tostring(sensorevent.values[0]));                 magneticy.settext(float.tostring(sensorevent.values[1]));                 magneticz.settext(float.tostring(sensorevent.values[2]));                  double teslaxyz = (math.sqrt((magx * magx) + (magy * magy)                         + (magz * magz)));                 magnetict.settext(double.tostring(teslaxyz));                  try {                     jsonobject tesla = new jsonobject();                     tesla.put("tesla", teslaxyz);                     telsastring = tesla.tostring();                      new myasynctask().execute(telsastring);                  } catch (jsonexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 }              }         }      }      class myasynctask extends asynctask<string, integer, string> {          @override         protected string doinbackground(string... params) {             // todo auto-generated method stub             bufferedreader reader = null;              try {                 system.out.println("the output of : doinbackground "                         + params[0]);                  url myurl = new url(                         "https://serverside-rhcloud.com/webapi/tesla");                  httpurlconnection conn = (httpurlconnection) myurl                         .openconnection();                 conn.setrequestmethod("post");                 conn.setdooutput(true);                 conn.setconnecttimeout(10000);                 conn.setreadtimeout(10000);                 conn.setrequestproperty("content-type", "application/json");                 conn.connect();                    // create data output stream                 dataoutputstream wr = new dataoutputstream(                         conn.getoutputstream());                 // write output stream string                 wr.writebytes(params[0]);                  wr.close(); //              system.out.println("the output of getresponsecode: " //              + conn.getresponsecode());                   stringbuilder sb = new stringbuilder();                 reader = new bufferedreader(new inputstreamreader(                         conn.getinputstream()));                 string line;                  while ((line = reader.readline()) != null) {                     sb.append(line + "\n");                  }                  // system.out.println("the output of stringbulder before "                 // + routes);                 system.out.println("the output of stringbulder: "                         + sb.tostring());              } catch (ioexception e) {                  e.printstacktrace();                 return null;             } {                 if (reader != null) {                     try {                         reader.close();                         return null;                     } catch (exception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                 }             }              return null;          }          protected void onpostexecute(string result) {              new handler().postdelayed(new runnable() {                 @override                 public void run() {                     new myasynctask().execute(telsastring);                 }             }, 5 * 1000);          }      } 

first, invoking asynctask on every sensor reading. in cases, going sensor readings more once every 5 seconds.

second, also invoking asynctask onpostexecute(), generating memory-leaking handler , using postdelayed(). adds pile of tasks.

third, have no means of stopping of this. code execute until process terminates. doing network i/o every 5 seconds indefinitely unlikely make users happy.

fourth, not have adequate thread safety surrounding telsastring, being used multiple threads, simultaneously.


let's ignore issue of whether doing network i/o every 5 seconds cause users attempt murder you, , focus on rest.

if want bit of work every 5 seconds on background thread, already-running process, use scheduledexecutorservice. don't have fuss handler, don't have fuss asynctask.

have sensoreventlistener cache last sensor readings. have kick off scheduledexecutorservice only if not running, not on every reading. use synchronized or other java thread safety mechanisms ensure either sensoreventlistener updating readings or scheduledexecutorservice consuming readings @ 1 moment, not both simultaneously.

when comes time cease work, can shutdown() or shutdownnow() scheduledexecutorservice, after first unregistering sensoreventlistener.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -