简体   繁体   中英

progressdialog using asynctask fecthing data

can you help me, my code running before i tried to implement asynctask to show progressdialog in my application, it's been a since week,I tried several solutions but still the same problem, Please can someone correct my code, My code is:

package coma.exame.dbselect;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TableRow.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {
    InputStream is = null;
      String result = null;
      JSONObject contact = null;

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
               Button button = (Button) findViewById(R.id.button1);
               button.setOnClickListener(new View.OnClickListener()
                  {
                        public void onClick(View view) 
                    {
                    new task().execute();

                 }
                 });
           }

    class task extends AsyncTask<String, String, String>
           {
           private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);//private ProgressDialog progressDialog = new ProgressDialog(Login.this);
           protected String doInBackground(String... params) {
                   StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();StrictMode.setThreadPolicy(policy);        
               try{
                       HttpClient httpclient = new DefaultHttpClient();
                       HttpPost httppost = new HttpPost("http://192.168.1.11/sqlserv/test.php");
                       HttpResponse response = httpclient.execute(httppost); 
                       HttpEntity entity = response.getEntity();
                       is = entity.getContent();
               }
               catch(Exception e)
               {
                       Log.e("log_bad_connection", "Error in http connection "+e.toString());
                      Toast.makeText(getApplicationContext(), "Tsy tafa ny connexion", Toast.LENGTH_SHORT).show();
               }
                           try
                       {
                               BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                               StringBuilder sb = new StringBuilder();
                               String line = null;
                               while ((line = reader.readLine()) != null) 
                               {
                                       sb.append(line + "\n");

                               }
                               is.close();

                               result=sb.toString();
                               return result;
                       }
                       catch(Exception e)
                       {
                              Log.e("log_conversion_resultat", "Error converting result "+e.toString());
                           Toast.makeText(getApplicationContext(), " Input reading fail", Toast.LENGTH_SHORT).show();

                       }


           }

protected void onPreExecute() {
    progressDialog.setMessage("Download data...");
    progressDialog.show();

  }

               protected void onPostExecute(String result) {

                  JSONArray jArray = new JSONArray(result);

                       TableLayout tv=(TableLayout) findViewById(R.id.table);
                       tv.removeAllViewsInLayout();

                   for(int i=-1;i<jArray.length();i++)

                               {
                                       TableRow tr = new TableRow(MainActivity.this);

                                       tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                                      try {

                                           JSONObject json_data = jArray.getJSONObject(i);

                                   TextView b=new TextView(MainActivity.this);
                                      String stime=String.valueOf(json_data.getString("AR_REF"));
                                         b.setText(stime);
                                       b.setTextColor(Color.BLACK);
                                       b.setTextSize(6);
                                       tr.addView(b);
                                       tv.addView(tr);
                               final View vline1 = new View(MainActivity.this);
                             vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
                             vline1.setBackgroundColor(Color.RED);
                             tv.addView(vline1);       

                                       }

                           }
                   this.progressDialog.dismiss(); 
                     }

                      catch(JSONException e)
                       {
                               Log.e("log_tag", "Error parsing data "+e.toString());
                               Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
                       }

}

}
}

My logcat showing error :

05-31 12:13:09.437: I/Choreographer(698): Skipped 64 frames! The application may be doing too much work on its main thread. 05-31 12:13:10.159: I/Choreographer(698): Skipped 60 frames! The application may be doing too much work on its main thread. 05-31 12:13:10.619: D/gralloc_goldfish(698): Emulator without GPU emulation detected. 05-31 12:13:25.900: I/Choreographer(698): Skipped 111 frames! The application may be doing too much work on its main thread. 05-31 12:13:26.334: I/Choreographer(698): Skipped 35 frames! The application may be doing too much work on its main thread. 05-31 12:13:26.808: I/Choreographer(698): Skipped 50 frames! The application may be doing too much work on its main thread. 05-31 12:13:27.214: I/Choreographer(698): Skipped 106 frames! The application may be doing too much work on its main thread. 05-31 12:13:27.413: I/Choreographer(698): Skipped 41 frames! The application may be doing too much work on its main thread. 05-31 12:13:27.613: I/Choreographer(698): Skipped 32 frames! The application may be doing too much work on its main thread. 05-31 12:13:27.844: I/Choreographer(698): Skipped 58 frames! The application may be doing too much work on its main thread. 05-31 12:13:28.113: I/Choreographer(698): Skipped 59 frames! The application may be doing too much work on its main thread. 05-31 12:13:28.364: I/Choreographer(698): Skipped 57 frames! The application may be doing too much work on its main thread. 05-31 12:13:28.634: I/Choreographer(698): Skipped 55 frames! The application may be doing too much work on its main thread. 05-31 12:13:28.894: I/Choreographer(698): Skipped 62 frames! The application may be doing too much work on its main thread. 05-31 12:13:29.178: I/Choreographer(698): Skipped 64 frames! The application may be doing too much work on its main thread. 05-31 12:13:29.484: I/Choreographer(698): Skipped 75 frames! The application may be doing too much work on its main thread. 05-31 12:13:29.754: I/Choreographer(698): Skipped 57 frames! The application may be doing too much work on its main thread. 05-31 12:13:30.034: I/Choreographer(698): Skipped 35 frames! The application may be doing too much work on its main thread. 05-31 12:13:30.294: I/Choreographer(698): Skipped 53 frames! The application may be doing too much work on its main thread. 05-31 12:13:30.573: I/Choreographer(698): Skipped 66 frames! The application may be doing too much work on its main thread. 05-31 12:13:30.804: I/Choreographer(698): Skipped 53 frames! The application may be doing too much work on its main thread. 05-31 ...

Thank you

Can follow this:

public class ReadWebpageAsyncTask extends Activity {
private TextView textView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.TextView01);
}

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
  String response = "";
  for (String url : urls) {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    try {
      HttpResponse execute = client.execute(httpGet);
      InputStream content = execute.getEntity().getContent();

      BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
      String s = "";
      while ((s = buffer.readLine()) != null) {
        response += s;
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  return response;
}

@Override
protected void onPostExecute(String result) {
  textView.setText(result);
 }
}

public void onClick(View view) {
  DownloadWebPageTask task = new DownloadWebPageTask();
   task.execute(new String[] { "http://www.vogella.com" });

  }
} 

Firstly, that is not an error. That's an INFO in the Logcat which is warning you about some intensive task you're doing in your UI thread. As I see you've already used AsyncTask for your CPU intensive task. You're using an emulator and skipping frames will be a problem on the emulator. If it skips too many, you need to look what CPU intensive task you're doing on UI thread. Test it on an actual device and it'll probably not show that warning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM