我创建了一个名为ConnectionHttp的自定义AsyncTask类 . 当主活动(test1.java)中的按钮调用时,必须返回Json String . 一切都没关系,我可以得到字符串 .
关键是我想在后台任务运行时在主活动上显示ProgressDialog,但它不起作用 . 我没有看到任何ProgressDialog .
1)我如何修复progressDialog
2)我如何解决日志中的问题(应用程序可能做了太多工作......)(我在真实设备上测试)
这是我的代码:
test1.java(主要活动)
public class test1 {public static final String URL =“.....”;
私人ProgressDialog pDialog;
私人TextView电视;
@覆盖
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_test1);
pDialog = new ProgressDialog(test1.this);
tv =(TextView)findViewById(R.id.tvTest);
按钮btRun =(按钮)findViewById(R.id.btTestRun);
btRun.setOnClickListener(new View.OnClickListener(){
@覆盖
public void onClick(查看v){
尝试{
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.java
public class ConnectionHttp扩展AsyncTask {private ProgressDialog pDialog;
私有上下文;
private String LOGIN_URL;
private JSONParser jsonParser;
//标签
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();
}
@覆盖
public void onPreExecute(){
super.onPreExecute();
pDialog.setMessage(“Connexion au serveur remote ...”);
pDialog.setIndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@覆盖
public String doInBackground(String ... args){
// TODO自动生成的方法存根
成功;
字符串值=“”;
尝试{
List <NameValuePair> params = new ArrayList <>();
//添加参数
// ...
params.add(.....);
//
Log.d(“请求#1”,“开始”);
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL,“POST”,params);
// json的成功标记
success = json.getInt(TAG_SUCCESS);
if(success == 1){
Log.d(“请求#1”,“成功”);
values = json.getString(TAG_VALUES);
return json.getString(TAG_MESSAGE);
}其他{
return json.getString(TAG_MESSAGE);
}
} catch(JSONException e){
e.printStackTrace();
}
回报值;
}
public void onPostExecute(String message){
Log.d(“请求#1”,“完成 . ”);
pDialog.setMessage( “完成的 . ”);
pDialog.dismiss();
}
Android日志
D /请求#1:启动D /请求#1:成功! I / Choreographer:跳过49帧!应用程序可能在其主线程上做了太多工作 . D /请求#1:完成
2 回答
不要将进度对话框传递给Async类 . 而是在Async类上创建它 .
修改 onPreExecute 到此
从test1.java中删除进度对话框
首先从AsyncTask中删除进度对话框,然后使用传递的上下文创建ProgressDialog;