你能幫我嗎,我的代碼運行之前,我試圖實施asynctask在我的應用程序中顯示progressdialog,這是一個自從一週以來,我嘗試了幾個解決方案,但仍然是同樣的問題,請有人糾正我代碼, 我的代碼是:progressdialog使用asynctask fecthing數據
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的表示錯誤:
12月5日至31日:13:09.437:I /指導(698):跳過64幀! 應用程序可能在其主線程上做了太多工作。 05-31 12:13:10.159:I /編舞師(698):跳過60幀! 應用程序可能在其主線程上做了太多工作。 05-31 12:13:10.619:D/gralloc_goldfish(698):沒有GPU仿真的仿真器 檢測到。 05-31 12:13:25.900:我/編舞(698):跳過111 幀!該應用程序可能在其主要的 線程上做了太多工作。 05-31 12:13:26.334:I /編舞師(698):跳過35幀! 該應用程序可能在其主線程上做了太多工作。 05-31 12:13:26.808:I/Choreographer(698):跳過了50幀! 應用程序可能在其主線程上做了太多工作。 05-31 12:13:27.214:I /編舞師(698):跳過106幀! 應用程序可能在其主線程上做了太多工作。 05-31 12:13:27.413:I /編舞師(698):跳過41幀! 應用程序可能在其主線程上做了太多工作。 05-31 12:13:27.613:I /編舞師(698):跳過32幀! 應用程序可能在其主線程上做了太多工作。 05-31 12:13:27.844:I /編舞師(698):跳過了58幀! 應用程序可能在其主線程上做了太多工作。 05-31 12:13:28.113:I /編舞師(698):跳過59幀! 應用程序可能在其主線程上做了太多工作。 05-31 12:13:28.364:I /編舞師(698):跳過了57幀! 應用程序可能在其主線程上做了太多工作。 05-31 12:13:28.634:I /編舞師(698):跳過55幀! 應用程序可能在其主線程上做了太多工作。 05-31 12:13:28.894:I /編舞師(698):跳過62幀! 應用程序可能在其主線程上做了太多工作。 05-31 12:13:29.178:I/Choreographer(698):跳過64幀! 應用程序可能在其主線程上做了太多工作。 05-31 12:13:29.484:I /編舞師(698):跳過75幀! 應用程序可能在其主線程上做了太多工作。 05-31 12:13:29.754:I /編舞師(698):跳過了57幀! 應用程序可能在其主線程上做了太多工作。 05-31 12:13:30.034:我/編舞師(698):跳過35幀! 應用程序可能在其主線程上做了太多工作。 05-31 12:13:30.294:I /編舞師(698):跳過53幀! 應用程序可能在其主線程上做了太多工作。 05-31 12:13:30.573:I /編舞師(698):跳過66幀! 應用程序可能在其主線程上做了太多工作。 05-31 12:13:30.804:I /編舞師(698):跳過53幀! 應用程序可能在其主線程上做了太多工作。 05-31 ...
謝謝
你能告訴我如何使用它嗎?因爲我已經在我的代碼中使用PreExecute方法 – Ratoj
好吧。我們知道您的應用程序在主線程中做了太多工作。 –
'ProgressDialog'是否顯示? –