你可以帮助我吗,我的代码运行之前我试图实现asynctask在我的应用程序中显示progressdialog,这是一周以来,我尝试了几个解决方案,但仍然是同样的问题,请有人纠正我的代码, 我的代码是:
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();
}
}
}
}
我的logcat显示错误:
05-31 12:13:09.437:I / Choreographer(698):跳过64帧!该 应用程序可能在其主线程上做了太多工作。 05-31 12:13:10.159:I / Choreographer(698):跳过60帧!该 应用程序可能在其主线程上做了太多工作。 05-31 12:13:10.619:D / gralloc_goldfish(698):没有GPU仿真的仿真器 检测。 05-31 12:13:25.900:I / Choreographer(698):跳过111 帧!应用程序可能在其主要方面做了太多工作 线。 05-31 12:13:26.334:I / Choreographer(698):跳过35帧! 应用程序可能在其主线程上做了太多工作。 05-31 12:13:26.808:I / Choreographer(698):跳过50帧!该 应用程序可能在其主线程上做了太多工作。 05-31 12:13:27.214:I / Choreographer(698):跳过106帧!该 应用程序可能在其主线程上做了太多工作。 05-31 12:13:27.413:I / Choreographer(698):跳过41帧!该 应用程序可能在其主线程上做了太多工作。 05-31 12:13:27.613:I / Choreographer(698):跳过32帧!该 应用程序可能在其主线程上做了太多工作。 05-31 12:13:27.844:I / Choreographer(698):跳过58帧!该 应用程序可能在其主线程上做了太多工作。 05-31 12:13:28.113:I / Choreographer(698):跳过59帧!该 应用程序可能在其主线程上做了太多工作。 05-31 12:13:28.364:I / Choreographer(698):跳过57帧!该 应用程序可能在其主线程上做了太多工作。 05-31 12:13:28.634:I / Choreographer(698):跳过55帧!该 应用程序可能在其主线程上做了太多工作。 05-31 12:13:28.894:I / Choreographer(698):跳过62帧!该 应用程序可能在其主线程上做了太多工作。 05-31 12:13:29.178:I / Choreographer(698):跳过64帧!该 应用程序可能在其主线程上做了太多工作。 05-31 12:13:29.484:I / Choreographer(698):跳过75帧!该 应用程序可能在其主线程上做了太多工作。 05-31 12:13:29.754:I / Choreographer(698):跳过57帧!该 应用程序可能在其主线程上做了太多工作。 05-31 12:13:30.034:I / Choreographer(698):跳过35帧!该 应用程序可能在其主线程上做了太多工作。 05-31 12:13:30.294:I / Choreographer(698):跳过53帧!该 应用程序可能在其主线程上做了太多工作。 05-31 12:13:30.573:I / Choreographer(698):跳过66帧!该 应用程序可能在其主线程上做了太多工作。 05-31 12:13:30.804:I / Choreographer(698):跳过53帧!该 应用程序可能在其主线程上做了太多工作。 05-31 ......
谢谢
答案 0 :(得分:0)
可以遵循:
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" });
}
}
答案 1 :(得分:0)
首先,这不是错误。这是Logcat中的INFO,它警告您在UI线程中执行的一些密集任务。正如我所见,您已经将AsyncTask
用于CPU密集型任务。您正在使用模拟器,跳过帧将是模拟器上的问题。如果它跳过太多,您需要查看您在UI线程上执行的CPU密集型任务。在实际设备上测试,它可能不会显示警告。