I'm executing AsyncTask that fetch data from my web host. In order for me to retrieve data, I need to re-open my app. How can I fetch data every second? I know that AsyncTask could only be executed once, but I needed it not only for my app but also to learn about this problem. Hope to learn from you guys.
By the way this is my code:
class AsyncDataClass2 extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params) {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(params[0]);
String jsonResult = "";
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", params[1]));
nameValuePairs.add(new BasicNameValuePair("password", params[2]));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return jsonResult;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
if (result.equals("") || result == null)
{
Toast msg = Toast.makeText(MapsActivity.this, "connection failed", Toast.LENGTH_LONG);
msg.setGravity(Gravity.CENTER, 0, 0);
msg.show();
}
else
{
Toast msg = Toast.makeText(MapsActivity.this, "connected", Toast.LENGTH_LONG);
msg.setGravity(Gravity.CENTER,0,0);
msg.show();
}
try {
JSONArray json = new JSONArray(result);
for (int i = 0; i < json.length(); i++) {
JSONObject e = json.getJSONObject(i);
String point = e.getString("Point");
String[] point2 = point.split(",");
String devStatus = e.getString("Status"); //now, let's process the Status...
String strOwner = e.getString("Owner"); //now, let's process the owner...
//==============================================================================
if (devStatus.equals("fire")) {
IsThereFire=true;
}
} //---End of FOR LOOP---//
}//---end of TRY---//
catch (JSONException e)
{
e.printStackTrace();
}
}
private StringBuilder inputStreamToString(InputStream is)
{
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
try
{
while ((rLine = br.readLine()) != null)
{
answer.append(rLine);
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return answer;
}
}
EDIT:
Sir, this is the timer I used..
///---CODE for the TIMER that ticks every second and synch to database to update status---///
Thread t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
// HERE IS THE ACTUAL CODE WHERE I CALL THE ASYNCDATACLASS2
AsyncDataClass2 performBackgroundTask = new AsyncDataClass2();
// PerformBackgroundTask this class is the class that extends AsynchTask
performBackgroundTask.execute();
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
//---------------------------------------------------------------//