我正在尝试在doInBackground()方法中执行一些服务器端功能,并且根据服务器的结果我必须执行几个步骤,我已经在onPostExecute()中写了它们但是对于我onPostExecute()正在执行之前完成doInBackground()
protected String[] doInBackground(String... params) {
final String ccode = params[0];
final String mobile = params[1];
final String img= params[2];
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
respon = response;
Toast.makeText(getApplicationContext(), respon, Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put(KEY_MOB,mobile);
params.put(KEY_COUNTRY,ccode);
params.put("pic",img);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(stringRequest);
String[] s = {params[0],params[1],params[2]};
return s;
}
@Override
protected void onPostExecute(String[] result) {
pd.dismiss();
Toast.makeText(getApplicationContext(), "22222", Toast.LENGTH_SHORT).show();
System.out.print("+++++++++++++++++++++++++***************"+respon);
if(respon.equals("success")) {
ob.register_user(m_n,cntry,nme,p_pic);
Intent h = new Intent(User_detais.this, home.class);
startActivity(h);
finish();
} else{
android.app.AlertDialog.Builder builder1 = new android.app.AlertDialog.Builder(context);
builder1.setMessage("Please Check your Data Connection and try again.");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
android.app.AlertDialog alert11 = builder1.create();
alert11.show();
}
答案 0 :(得分:0)
这种情况正在发生,因为您在doInBackground
中使用了Volley。 Volley的请求本身是异步的,不会阻止UI。因此,一旦doInBackground
中的代码运行,它就不会等待请求完成。
你应该做的是避免这个AsyncTask
并在onResponse
和onErrorResponse
中随意做任何你想做的事。