2014-09-30 101 views
0

我正在做一個android窗體中字段的驗證。我正在用服務器檢查用戶名是否在服務器中可用。但是主線程會在下一頁之前進入下一頁異步檢查完成。UI線程在android中運行異步類之前運行

代碼:

btnnext1.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

     isallValid=true; 
     //check second time for username validation(first time was in onfocus changed) 

//  if(txtusername.getText().toString().trim().equals("achuthan")){ 
//   txtusername.setError("Username exsists!"); 
//   isUsernameValid=false; 
//  } 
//  
//  else 
//  { 
//   isUsernameValid=true; 
//  } 

     try { 
      Void async_result=new validateusername().execute().get(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ExecutionException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

       if(txtfullname.getText().toString().trim().length()==0) 
      { 
      txtfullname.requestFocus(); 
      txtfullname.setError("Field required"); 
      isallValid=false; 

      } 
     if(txtdob.getText().toString().trim().length()==0) 
     { 
      txtdob.requestFocus(); 
      txtdob.setError("Field required"); 
      isallValid=false; 
     } 
     if(txtoccupation.getText().toString().trim().length()==0) 
     { 
      txtoccupation.requestFocus(); 
      txtoccupation.setError("Field required"); 
      isallValid=false; 
     } 
     if(txtusername.getText().toString().trim().length()<6){ 
      txtusername.requestFocus(); 
      txtusername.setError("Minimum length of 6 characters"); 
      isallValid=false; 
     } 
     if(txtpassword.getText().toString().trim().length()==0) 
     { 
      txtpassword.requestFocus(); 
      txtpassword.setError("Field required"); 
      isallValid=false; 
     } 
     if(txtconfirmpassword.getText().toString().trim().length()==0) 
     { 
      txtconfirmpassword.requestFocus(); 
      txtconfirmpassword.setError("Field required"); 
      isallValid=false; 
     } 
     else if(!txtpassword.getText().toString().trim().equals(txtconfirmpassword.getText().toString().trim())) 
     { 
      //txtconfirmpassword.requestFocus(); 
      txtconfirmpassword.setError("Passwords not equal"); 
      txtpassword.setError("Passwords not equal"); 
      isallValid=false; 
     } 


     if(isallValid&&isUsernameValid) 
     { 
      //Toast.makeText(getActivity(),"VALID FORM!!",Toast.LENGTH_LONG).show(); 
      ((SignUpPage)getActivity()).getValues().fullname=txtfullname.getText().toString().trim(); 
      ((SignUpPage)getActivity()).getValues().dob=txtdob.getText().toString().trim(); 


      int id=radiogender.getCheckedRadioButtonId(); 
      RadioButton rb=(RadioButton) view.findViewById(id); 
      String gender=rb.getText().toString(); 
      ((SignUpPage)getActivity()).getValues().gender=gender; 


      int id1=radiomarital.getCheckedRadioButtonId(); 
      RadioButton rb1=(RadioButton) view.findViewById(id1); 
      String marital_status=rb1.getText().toString(); 
      ((SignUpPage)getActivity()).getValues().marital_status=marital_status; 



      ((SignUpPage)getActivity()).getValues().occupation=txtoccupation.getText().toString().trim(); 
      ((SignUpPage)getActivity()).getValues().username=txtusername.getText().toString().trim(); 
      ((SignUpPage)getActivity()).getValues().password=txtpassword.getText().toString().trim(); 



      ((SignUpPage)getActivity()).selectFragment(1); 


     } 
        //if all valid , store values and go to next fragment 
        //((SignUpPage)getActivity()).selectFragment(1); 
      } 
     }); 



     return view; 
    } 

異步類:

public class validateusername extends AsyncTask<String,Void,Void> 
     { 

      @Override 
      protected Void doInBackground(String... arg0) { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(URL); 
       List<NameValuePair> pairs = new ArrayList<NameValuePair>(); 
       pairs.add(new BasicNameValuePair("username",txtusername.getText().toString().trim())); 
       try { 
        httppost.setEntity(new UrlEncodedFormEntity(pairs)); 
        response = httpclient.execute(httppost); 
        result=responsetostring.getResponseBody(response); 
       } catch (ClientProtocolException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 




       return null; 
      } 
      @Override 
      protected void onPostExecute(Void result1) { 
       try { 
        jsonobj=new JSONObject(result); 
        job2=jsonobj.getJSONObject("server_message"); 
       } catch (JSONException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } 
       try { 
        finalresult=job2.getString("username_availability_message"); 
       } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       if(finalresult.equals("exist")){ 
        txtusername.setError("Username exsists!"); 
        isUsernameValid=false; 
       } 
       else if(finalresult.equals("available")) 
       { 

        isUsernameValid=true; 
       } 
      } 

     } 

我使用GET方法試圖使主線程等待,直到異步類完成,但它沒有work.Please幫助! !

回答

2

檢查只在點擊按鈕isallValid,然後執行此驗證您的onPostExecute(Void result)方法裏面,

if(isUsernameValid) 
    { 
     //Toast.makeText(getActivity(),"VALID FORM!!",Toast.LENGTH_LONG).show(); 
     ((SignUpPage)getActivity()).getValues().fullname=txtfullname.getText().toString().trim(); 
     ((SignUpPage)getActivity()).getValues().dob=txtdob.getText().toString().trim(); 


     int id=radiogender.getCheckedRadioButtonId(); 
     RadioButton rb=(RadioButton) view.findViewById(id); 
     String gender=rb.getText().toString(); 
     ((SignUpPage)getActivity()).getValues().gender=gender; 


     int id1=radiomarital.getCheckedRadioButtonId(); 
     RadioButton rb1=(RadioButton) view.findViewById(id1); 
     String marital_status=rb1.getText().toString(); 
     ((SignUpPage)getActivity()).getValues().marital_status=marital_status; 



     ((SignUpPage)getActivity()).getValues().occupation=txtoccupation.getText().toString().trim(); 
     ((SignUpPage)getActivity()).getValues().username=txtusername.getText().toString().trim(); 
     ((SignUpPage)getActivity()).getValues().password=txtpassword.getText().toString().trim(); 



     ((SignUpPage)getActivity()).selectFragment(1); 


    } 

現在將工作.................

+0

但是我調用了兩次async類。一次在用戶名字段的onfocuschanged偵聽器中。所以post執行也會在這種情況下運行。 – 2014-09-30 07:08:33

+0

你可以發佈onfocuschanged偵聽器代碼嗎? – 2014-09-30 07:13:03

0

請勿使用get()調用AsyncTask,它會掛起您的UI。

於是打電話給你的AsyncTask一樣,

String userName = txtusername.getText().toString().trim(); 
new validateusername().execute(userName);  // pass the String from EditText, as you cannot interact with UI in doInBackground 

修改您的AsyncTask像

public class validateusername extends AsyncTask<String,Void,String> 
    { 

     @Override 
     protected String doInBackground(String... arg0) { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(URL); 
      List<NameValuePair> pairs = new ArrayList<NameValuePair>(); 
      pairs.add(new BasicNameValuePair("username",arg0[0])); // arg0[0] is the username passed from AsyncTask call 
      try { 

       httppost.setEntity(new UrlEncodedFormEntity(pairs)); 
       response = httpclient.execute(httppost); 
       result=responsetostring.getResponseBody(response); 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 




      return result; 
     } 



     @Override 
     protected void onPostExecute(String result1) { 
      try { 
       jsonobj=new JSONObject(result); 
       job2=jsonobj.getJSONObject("server_message"); 
      } catch (JSONException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      try { 
       finalresult=job2.getString("username_availability_message"); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      if(finalresult.equals("exist")){ 
       txtusername.setError("Username exsists!"); 
       isUsernameValid=false; 
      } 
      else if(finalresult.equals("available")) 
      { 

       isUsernameValid=true; 
      } 





if(txtfullname.getText().toString().trim().length()==0) 
    { 
    txtfullname.requestFocus(); 
    txtfullname.setError("Field required"); 
    isallValid=false; 

    } 
if(txtdob.getText().toString().trim().length()==0) 
{ 
    txtdob.requestFocus(); 
    txtdob.setError("Field required"); 
    isallValid=false; 
} 
if(txtoccupation.getText().toString().trim().length()==0) 
{ 
    txtoccupation.requestFocus(); 
    txtoccupation.setError("Field required"); 
    isallValid=false; 
} 
if(txtusername.getText().toString().trim().length()<6){ 
    txtusername.requestFocus(); 
    txtusername.setError("Minimum length of 6 characters"); 
    isallValid=false; 
} 
if(txtpassword.getText().toString().trim().length()==0) 
{ 
    txtpassword.requestFocus(); 
    txtpassword.setError("Field required"); 
    isallValid=false; 
} 
if(txtconfirmpassword.getText().toString().trim().length()==0) 
{ 
    txtconfirmpassword.requestFocus(); 
    txtconfirmpassword.setError("Field required"); 
    isallValid=false; 
} 
else if(!txtpassword.getText().toString().trim().equals(txtconfirmpassword.getText().toString().trim())) 
{ 
    //txtconfirmpassword.requestFocus(); 
    txtconfirmpassword.setError("Passwords not equal"); 
    txtpassword.setError("Passwords not equal"); 
    isallValid=false; 
} 


if(isallValid&&isUsernameValid) 
{ 
    //Toast.makeText(getActivity(),"VALID FORM!!",Toast.LENGTH_LONG).show(); 
    ((SignUpPage)getActivity()).getValues().fullname=txtfullname.getText().toString().trim(); 
    ((SignUpPage)getActivity()).getValues().dob=txtdob.getText().toString().trim(); 


    int id=radiogender.getCheckedRadioButtonId(); 
    RadioButton rb=(RadioButton) view.findViewById(id); 
    String gender=rb.getText().toString(); 
    ((SignUpPage)getActivity()).getValues().gender=gender; 


    int id1=radiomarital.getCheckedRadioButtonId(); 
    RadioButton rb1=(RadioButton) view.findViewById(id1); 
    String marital_status=rb1.getText().toString(); 
    ((SignUpPage)getActivity()).getValues().marital_status=marital_status; 



    ((SignUpPage)getActivity()).getValues().occupation=txtoccupation.getText().toString().trim(); 
    ((SignUpPage)getActivity()).getValues().username=txtusername.getText().toString().trim(); 
    ((SignUpPage)getActivity()).getValues().password=txtpassword.getText().toString().trim(); 



    ((SignUpPage)getActivity()).selectFragment(1); 


    } 
} 

onPostExecute(),檢查的條件,並導航到下一個活動。