0

我正在使用HTTPAsyncClient向服務器發送一個post請求,並且它是通過按下按鈕(命名複選框)激活的,現在問題出現在我按第一次時它跳過進入TextHttpResponseHandler( ),所以它不會向服務器發送任何東西,但是在第二次按下時,它會正常進入函數並調用服務器,當我切換到另一個活動時,它也會執行相同的操作並跳過響應 處理程序。第一次跳過HTTPAsyncClient

編輯:我正在調試程序,我意識到它不會像第一次運行那樣跳過部件,它根本不調用服務器,並且返回server_response = null,但是在第二次調用它時調用它服務器和一切正常

編輯2:進一步查看我的代碼與調試,我意識到真正的問題是,AsyncHttpClient客戶端=新的AsyncHttpClient();需要時間來初始化,這就是爲什麼響應不會一開始就出現,因爲沒有實際的服務器調用發送,但第二次,AsyncHttpClient客戶端=新的AsyncHttpClient();被初始化,建立連接,這就是爲什麼它給出了一個迴應,並正常動作,現在的問題是我怎麼解決這個問題,使之無縫工作

這裏是代碼:

public class RegisterFragment extends Fragment { 
ProgressBar progressBar; 
ImageView checkbutton; 
EditText first_name_ET; 
EditText last_name_ET; 
EditText email_ET; 
EditText password_ET; 
EditText confirm_password_ET; 
EditText phone_ET; 
EditText username_ET; 
String first_name; 
String last_name; 
String email; 
String password; 
String confirm_password; 
String phone; 
String username; 
Pattern pattern; 
Matcher matcher; 
String URL = "http://198.58.109.238/engezni/public/android/register"; 
String USER_PREF = "User Pref"; 
String server_response = null; 
String response_function_result = null; 


public RegisterFragment() { 
    // Required empty public constructor 
} 

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    final View view = inflater.inflate(R.layout.fragment_register, container, false); 
    progressBar = (ProgressBar) view.findViewById(R.id.progressbar); 
    getView(); 
    progressBar.setVisibility(View.GONE); 

    if (view != null) { 
     first_name_ET = (EditText) view.findViewById(R.id.first_name_ET); 
     last_name_ET = (EditText) view.findViewById(R.id.last_name_ET); 
     email_ET = (EditText) view.findViewById(R.id.email_ET); 
     password_ET = (EditText) view.findViewById(R.id.password_ET); 
     confirm_password_ET = (EditText) view.findViewById(R.id.confirm_password_ET); 
     phone_ET = (EditText) view.findViewById(R.id.phone_ET); 
     username_ET = (EditText) view.findViewById(R.id.username_ET); 
     checkbutton = (ImageView) view.findViewById(R.id.check_button); 

    } 

    first_name = first_name_ET.getText().toString().trim(); 
    last_name = last_name_ET.getText().toString().trim(); 
    email = email_ET.getText().toString().trim(); 
    password = password_ET.getText().toString().trim(); 
    confirm_password = confirm_password_ET.getText().toString().trim(); 
    phone = phone_ET.getText().toString().trim(); 
    username = username_ET.getText().toString().trim(); 


    checkbutton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (Validate()) { 
       response_function_result = null; 
       response_function_result = SendToServer(first_name, last_name, email, password, phone, username); 

       if (response_function_result != null) { 

        if (ServerErrorHandler(response_function_result)) { 
       /*Saving the fields in shared prefs and going to another activity*/ 
         SharedPreferences.Editor save = getActivity().getSharedPreferences(USER_PREF, 0).edit(); 
         save.putString("User Name", first_name + " " + last_name); 
         save.putString("Email", email); 
         save.putString("Password", password); 
         save.putString("Phone", phone); 
         save.putString("Name", username); 
         save.commit(); 

         Intent intent = new Intent(getActivity(), SignInScreen.class); 
         startActivity(intent); 

        } 

       } 
      } 
     } 
    }); 


    return view; 
} 

public boolean Validate() { 
    first_name = first_name_ET.getText().toString().trim(); 
    last_name = last_name_ET.getText().toString().trim(); 
    email = email_ET.getText().toString().trim(); 
    password = password_ET.getText().toString().trim(); 
    confirm_password = confirm_password_ET.getText().toString().trim(); 
    phone = phone_ET.getText().toString().trim(); 
    username = username_ET.getText().toString().trim(); 
    final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; 
    pattern = Pattern.compile(EMAIL_PATTERN); 
    matcher = pattern.matcher(email); 

    if (first_name.length() < 4 || first_name.length() > 30) { 
     first_name_ET.setError("First name should be 4 chartacters or more"); 
     return false; 
    } else { 
     first_name_ET.setError(null); 
    } 

    if (last_name.length() < 4 || last_name.length() > 30) { 
     last_name_ET.setError("First name should be 4 chartacters or more"); 
     return false; 
    } else { 
     last_name_ET.setError(null); 
    } 

    if (!matcher.matches()) { 
     email_ET.setError("Invalid Email ex:[email protected]"); 
     return false; 
    } else { 
     email_ET.setError(null); 
    } 
    if (password.length() < 6) { 
     password_ET.setError("Password has to be 6 characters or more"); 
     return false; 
    } else { 
     password_ET.setError(null); 
    } 

    if (!confirm_password.equals(password)) { 
     confirm_password_ET.setError("Password does not match"); 
     return false; 
    } else { 
     confirm_password_ET.setError(null); 
    } 

    if (phone.length() < 11) { 
     phone_ET.setError("Phone number invalid"); 
     return false; 
    } else { 
     phone_ET.setError(null); 
    } 


    return true; 
} 

public String SendToServer(String first_name, String last_name, String email, String password, String phone, String username) { 
    AsyncHttpClient client = new AsyncHttpClient(); 
    StringEntity stringEntity = null; 
    JsonArray jsonArray = new JsonArray(); 
    JsonObject jsonObject = new JsonObject(); 

    try { 
     jsonObject.addProperty("username", first_name + last_name); 
     jsonObject.addProperty("email", email); 
     jsonObject.addProperty("password", password); 
     jsonObject.addProperty("name", username); 
     jsonObject.addProperty("phone", phone); 
     jsonObject.addProperty("dop", "dummy DOP"); 
     jsonArray.add(jsonObject); 
     stringEntity = new StringEntity(jsonArray.toString()); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } 

    client.post(getActivity().getApplicationContext(), URL, stringEntity, "application/json", new TextHttpResponseHandler() { 
     @Override 
     public void onStart() { 
      super.onStart(); 
      progressBar.setVisibility(View.VISIBLE); 
     } 

     @Override 
     public void onFinish() { 
      super.onFinish(); 
      progressBar.setVisibility(View.GONE); 
     } 

     @Override 
     public void onFailure(int i, Header[] headers, String s, Throwable throwable) { 
      Toast.makeText(getActivity(), "onfaaail", Toast.LENGTH_LONG).show(); 

     } 

     @Override 
     public void onSuccess(int i, Header[] headers, String s) { 

      server_response = s; 
      SharedPreferences.Editor save = getActivity().getSharedPreferences(USER_PREF, 0).edit(); 
      save.putString("Server Response", server_response); 
      save.commit(); 


     } 
    }); 

    return server_response; 

} 

public boolean ServerErrorHandler(String response) { 
    String error_message = "Error Message: "; 

    // Checks for errors. 
    if (response.contains("INVALID") || response.contains("EXISTS")) { 
     // error occured. 
     if (response.contains("EMAIL_INVALID")) { 
      error_message = error_message + " Invalid Email"; 
     } 
     if (response.contains("PASSWORD_INVALID")) { 
      error_message = error_message + " Invalid Password"; 
     } 
     if (response.contains("PHONE_INVALID")) { 
      error_message = error_message + " Invalid Phone"; 
     } 
     if (response.contains("NAME_INVALID")) { 
      error_message = error_message + " Invalid Name"; 
     } 
     if (response.contains("DOP_INVALID")) { 
      error_message = error_message + " Invalid DoP"; 
     } 
     if (response.contains("USERNAME_INVALID")) { 
      error_message = error_message + " Invalid Username"; 
     } 
     if (response.contains("USERNAME_EXIST")) { 
      error_message = error_message + " Name Exists"; 
     } 
     if (response.contains("EMAIL_EXIST")) { 
      error_message = error_message + " Email Exists"; 
     } 
     if (response.contains("PHONE_EXIST")) { 
      error_message = error_message + " Phone Exists"; 
     } 

     Toast.makeText(getActivity().getApplicationContext(), error_message, Toast.LENGTH_LONG).show(); 


     return false; 
    } else { 
    /*No error*/ 
     Toast.makeText(getActivity().getApplicationContext(), "Registered", Toast.LENGTH_LONG).show(); 
     return true; 

    } 
} 
} 

回答

1

當你這樣做:response_function_result = SendToServer(first_name, last_name, email, password, phone, username);

向您的驗證服務器發出異步請求。這基本上意味着有一個後臺線程可以完成整個請求,並且主線程不會等待結果。因此,當驗證請求在後臺進行時,該行將立即執行,該行爲空,因此它將返回空值。

+0

是的,我注意到這個像10分鐘前,但問題是現在,我怎麼能解決這個問題?有沒有辦法讓它等待服務器爲我做好準備? –

+0

onSuccess方法在請求成功後執行,因此將所有需要來自響應中的值的內容放入onSuccess方法中。 –

+0

我會在一秒鐘之內嘗試這個,並將其標記爲已解決,如果它可行,我想它會 –