Android onPostExecute在doInBackground完成之前执行
我试图在doInBackground()方法中执行一些服务器端功能,根据服务器的结果,我必须执行几个步骤,我已经在onPostExecute()中编写了这些步骤,但对我来说,onPostExecute()是在doInBackground()完成之前执行的Android onPostExecute在doInBackground完成之前执行,android,performance,android-fragments,android-studio,Android,Performance,Android Fragments,Android Studio,我试图在doInBackground()方法中执行一些服务器端功能,根据服务器的结果,我必须执行几个步骤,我已经在onPostExecute()中编写了这些步骤,但对我来说,onPostExecute()是在doInBackground()完成之前执行的 protectedstring[]doInBackground(String…params){ 最终字符串ccode=params[0]; 最终字符串mobile=params[1]; 最终字符串img=params[2]; StringReq
protectedstring[]doInBackground(String…params){
最终字符串ccode=params[0];
最终字符串mobile=params[1];
最终字符串img=params[2];
StringRequest StringRequest=新建StringRequest(Request.Method.POST,REGISTER\u URL,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
respon=响应;
Toast.makeText(getApplicationContext(),respon,Toast.LENGTH_SHORT).show();
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
}){
@凌驾
受保护的映射getParams(){
Map params=新的HashMap();
参数put(按键移动,移动);
参数put(关键国家,ccode);
参数put(“pic”,img);
返回参数;
}
};
RequestQueue RequestQueue=Volley.newRequestQueue(上下文);
添加(stringRequest);
字符串[]s={params[0],params[1],params[2]};
返回s;
}
@凌驾
受保护的void onPostExecute(字符串[]结果){
pd.解散();
Toast.makeText(getApplicationContext(),“22222”,Toast.LENGTH_SHORT.show();
系统输出打印(“响应”);
如果(响应等于(“成功”)){
ob.注册用户(m\U n、cntry、nme、p\U pic);
意图h=新意图(用户_detais.this,home.class);
星触觉(h);
完成();
}否则{
android.app.AlertDialog.builder1=新的android.app.AlertDialog.Builder(上下文);
setMessage(“请检查您的数据连接并重试”);
builder1.setCancelable(true);
builder1.setPositiveButton(
“好的”,
新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
dialog.cancel();
}
});
android.app.AlertDialog alert11=builder1.create();
alert11.show();
}
发生这种情况是因为您在doInBackground
中使用了Volley。Volley的请求本身是异步的,不会阻塞UI。因此,一旦doInBackground
中的代码运行,它就不会等待请求完成
相反,您应该避免使用此AsyncTask
,只需在onResponse
和onErrorResponse
中执行您想执行的任何操作
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();
}