我正在做一個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幫助! !
但是我調用了兩次async類。一次在用戶名字段的onfocuschanged偵聽器中。所以post執行也會在這種情況下運行。 – 2014-09-30 07:08:33
你可以發佈onfocuschanged偵聽器代碼嗎? – 2014-09-30 07:13:03