我正在使用JSON HTTP
发出AsyncTask
请求。我在ProgressDialog
中创建了onPreExecute()
,我在onPostExecute()
中将其解雇了。问题是,diInBackground()
中有一个Toast消息,显示J response
和ProgressDialog
在Toast
出现之前被解雇,这意味着onPostExecute()
正在在生成Toast
之前调用。如何在Toast
出现之前解除它?
这是我的代码。
private class login extends AsyncTask<Void,Void,Void>{
private ProgressDialog progressDialog;
@Override
protected void onPreExecute(){
super.onPreExecute();
// Create & Start Progress Dialog
progressDialog = new ProgressDialog(LoginActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
String url = "http://api.openweathermap.org/data/2.5/weather?q=London,uk";
JsonObjectRequest req = new JsonObjectRequest(url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Toast.makeText(getApplicationContext(),response.toString() , Toast.LENGTH_SHORT).show();
}
}
catch (Exception e){
Toast.makeText(getApplicationContext(),"Server error!..",Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("from on error response Server Error..: ", error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(req);
return null;
}
@Override
protected void onPostExecute(Void v){
// Terminate Progress Dialog
progressDialog.dismiss();
}
}
答案 0 :(得分:1)
try {
Toast.makeText(getApplicationContext(),response.toString() , Toast.LENGTH_SHORT).show();
}
}
catch (Exception e){
Toast.makeText(getApplicationContext(),"Server error!..",Toast.LENGTH_SHORT).show();
}
这段代码完全错了。 您无法从doInBackground添加Toast。。你需要在解雇dioalog之后移动它
// Terminate Progress Dialog
progressDialog.dismiss();
//Add your Toast here
答案 1 :(得分:1)
无需在异步任务中编写Volley请求。您可以直接执行该任务,因为它内部使用线程。所以一旦你得到了响应或失败,那么你可以自己关闭对话框。我想这会对你有所帮助
String url = "http://api.openweathermap.org/data/2.5/weather?q=London,uk";
JsonObjectRequest req = new JsonObjectRequest(url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if(progressDialog.isShowing){
progressDialog.cancel();
}
try {
Toast.makeText(getApplicationContext(),response.toString() , Toast.LENGTH_SHORT).show();
}
}
catch (Exception e){
Toast.makeText(getApplicationContext(),"Server error!..",Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("from on error response Server Error..: ", error.getMessage());
if(progressDialog.isShowing){
progressDialog.cancel();
}
}
});
AppController.getInstance().addToRequestQueue(req);
我希望这对你有所帮助而不需要AsyncTask。
答案 2 :(得分:0)
您可以使用TimerTask在吐司节目后手动关闭对话框。您可以看到如何使用TimerTask HERE。您正在使用Toast.LENGTH_SHORT,因此吐司将显示2秒钟。
答案 3 :(得分:0)
关闭progressdialog里面的响应和错误监听器......不需要关闭post方法
private class login extends AsyncTask<Void,Void,Void>{
private ProgressDialog progressDialog;
@Override
protected void onPreExecute(){
super.onPreExecute();
// Create & Start Progress Dialog
progressDialog = new ProgressDialog(LoginActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
String url = "http://api.openweathermap.org/data/2.5/weather?q=London,uk";
JsonObjectRequest req = new JsonObjectRequest(url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Toast.makeText(getApplicationContext(),response.toString() , Toast.LENGTH_SHORT).show();
}
}
catch (Exception e){
Toast.makeText(getApplicationContext(),"Server error!..",Toast.LENGTH_SHORT).show();
progressDialog.dismiss(); add this line
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("from on error response Server Error..: ", error.getMessage());
progressDialog.dismiss(); add this line
}
});
AppController.getInstance().addToRequestQueue(req);
return null;
}
@Override
protected void onPostExecute(Void v){
// Terminate Progress Dialog
//progressDialog.dismiss(); comment this line
}
}
答案 4 :(得分:-1)
您想要打印吐司消息然后就可以使用了 处理程序。强>
Handler h = new Handler(this.getMainLooper());
h.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),response.toString() , Toast.LENGTH_SHORT).show();
//stopProgress
}
});