2016-06-07 115 views
-1

我試圖在doInBackground()方法中執行一些服務器端功能,並且依賴於服務器的結果我必須執行幾個步驟,我已經將它們寫入onPostExecute()我onPostExecute()doInBackground完成()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

這是因爲你在使用doInBackground凌空發生之前執行。 Volley的請求本身是異步的,不會阻止用戶界面。因此,運行doInBackground中的代碼後,它不會等待請求完成。

你應該做的是改變這個AsyncTask,並簡單地做任何你想要的東西,如onResponseonErrorResponse

相關問題