0

I try to update a string variable with data from a mysql database.


Example:

String str should grab a value from a mysql db using php. This is done and works perfectly.

But str should grab these values every 10 seconds, so it has to run in a loop.

And exactly this is the problem.

Connecting via httppost and getting entities blocks the UI, so it skip frames.

To solve this i used: Services, AsyncTasks and Runnables but always frame skipping.

This is the latest thing i tried:

public class AsyncStatus extends AsyncTask<String, String, String>{

     HttpResponse response;
     String str;
     HttpPost httppost;
     HttpClient httpclient;
     List<NameValuePair> nameValuePairs;
     Handler mHandler=null;  

      @Override
     protected String doInBackground(String... params) {
      // TODO Auto-generated method stub

     //Build the connection only once, to save performance     
      getStrFirst();    

         //Loop started
        final Runnable mUpdateUI = new Runnable() {
            public void run() {  
                System.out.println("called");                 
                try {
                      //to avoid "already consumed exceptions"
                      response = httpclient.execute(httppost);

                    //get the content
                    str =  EntityUtils.toString(response.getEntity());

                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }                      
                str = str.replaceAll("\\D", "");                  
                mHandler.postDelayed(this, 10000); // 10 seconds            
            }
        };
        mHandler.post(mUpdateUI); 
    return null;
}

public void getStrfirst(){
 nameValuePairs = new ArrayList<NameValuePair>();
    try
    {
      httpclient = new DefaultHttpClient();
      httppost = new HttpPost("http://lunation.square7.ch/msqlcount.php");
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      response = httpclient.execute(httppost); 
      str =  EntityUtils.toString(response.getEntity());

LOGCAT:

09-11 12:12:41.185: I/System.out(10874): called
09-11 12:12:41.975: I/Choreographer(10874): Skipped 51 frames!  The application may be doing too much work on its main thread.
09-11 12:12:51.990: I/System.out(10874): called

EDIT:

I call the AsyncTask with:

AsyncStatus assi= new AsyncStatus();
            assi.execute("nothing");

Why does this message appear, this isn't the main thread?

Thank you for any advice

vestland
  • 55,229
  • 37
  • 187
  • 305

0 Answers0