我创建了一个名为ConnectionHttp的自定义AsyncTask类。当主活动(test1.java)中的按钮调用时,必须返回Json String。一切都可以连接,我可以得到字符串。
关键是我想在后台任务运行时在主活动上显示ProgressDialog,但它不起作用。我没有看到任何ProgressDialog。
1)我如何修复progressDialog
2)我如何解决日志中的问题(应用程序可能做了太多工作......)(我在真实设备上进行测试)
这是我的代码:
public class test1 {
public static final String URL = "....."; private ProgressDialog pDialog; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test1); pDialog = new ProgressDialog(test1.this); tv = (TextView)findViewById(R.id.tvTest); Button btRun = (Button) findViewById(R.id.btTestRun); btRun.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { ConnectionHttp conn = new ConnectionHttp(test1.this, pDialog, URL); String str = conn.execute().get(); tv.setText(str); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }); }
公共类ConnectionHttp扩展了AsyncTask {
private ProgressDialog pDialog; private Context context; private String LOGIN_URL; private JSONParser jsonParser; // TAGS private final String TAG_SUCCESS = "success"; private final String TAG_MESSAGE = "message"; private final String TAG_VALUES = "values"; public ConnectionHttp(Context context, ProgressDialog pDialog, String url) { this.context = context; this.pDialog = pDialog; this.LOGIN_URL = url; this.jsonParser = new JSONParser(); } @Override public void onPreExecute() { super.onPreExecute(); pDialog.setMessage("Connexion au serveur distant..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } @Override public String doInBackground(String... args) { // TODO Auto-generated method stub int success; String values = ""; try { List<NameValuePair> params = new ArrayList<>(); // adding parameters // ... params.add(.....); // Log.d("request #1", "starting"); JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params); // success tag for json success = json.getInt(TAG_SUCCESS); if (success == 1) { Log.d("request #1", "Successfull"); values = json.getString(TAG_VALUES); return json.getString(TAG_MESSAGE); }else{ return json.getString(TAG_MESSAGE); } } catch (JSONException e) { e.printStackTrace(); } return values; } public void onPostExecute(String message) { Log.d("request #1", "done."); pDialog.setMessage("done."); pDialog.dismiss(); }
D /请求#1:开始
D /请求#1:成功!
I / Choreographer:跳过49帧!应用程序可能在其主线程上做了太多工作。D /请求#1:已完成
答案 0 :(得分:1)
不要将进度对话框传递给Async类。而是在Async类上创建它。
将 onPreExecute 修改为此
@Override
public void onPreExecute() {
super.onPreExecute();
pdialog = new ProgressDialog(getActivity(),"","Connexion au serveur distant...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
从test1.java删除进度对话框
答案 1 :(得分:0)
首先从AsyncTask中删除进度对话框,然后使用传递的上下文创建ProgressDialog;
public ConnectionHttp(Context context, ProgressDialog pDialog, String url) {
this.context = context;
this.pDialog = new ProgressDialog(context);
this.LOGIN_URL = url;
this.jsonParser = new JSONParser();
}