我正在开发一个Android应用程序。我托管了数据库和PHP文件,应用程序在模拟器中正常工作,URL提供了托管数据库和PHP文件的网站。但是,当我在真实设备中部署应用程序时,如果它是登录或注册,它不会从服务器获得任何类型的响应。下面是我用于寄存器的代码示例。
private void registerUser() {
final String username = Name.getText().toString().trim();
final String email = Email.getText().toString().trim();
final String password = Password.getText().toString().trim();
String genderString = "";
//Get the selected gender value from the radio buttons
RadioButton male = (RadioButton) findViewById(R.id.male);
boolean isMaleChecked = male.isChecked();
RadioButton female = (RadioButton) findViewById(R.id.female);
boolean isFemaleChecked = female.isChecked();
if(isMaleChecked){
genderString = "male";
}else if(isFemaleChecked){
genderString = "female";
}
final String _gender = genderString;
if (!email.matches(emailPattern) && password.length()<5) {
Toast.makeText(getApplicationContext(),"Invalid Email or Password is Short",Toast.LENGTH_SHORT).show();
} else {
progressDialog.setMessage("Registering user...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST,
Constants.URL_REGISTER,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
if(jsonObject.getBoolean("error")){
Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();
}else if (!jsonObject.getBoolean("error")){
Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();
finish();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.hide();
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("name", username);
params.put("email", email);
params.put("password", password);
params.put("gender", _gender);
return params;
}
};
RequestHandler.getInstance(this).addToRequestQueue(stringRequest);
// finish();
}
}
这是常量类。
public class Constants {
private static final String ROOT_URL = "http://singlover.000webhostapp.com/android/v1/";
//private static final String ROOT_URL = "http://192.168.8.101:8081/Android/v1/";
public static final String URL_REGISTER = ROOT_URL+"registerUser.php";
public static final String URL_LOGIN = ROOT_URL+"userLogin.php";
public static final String URL_ADDRESS = ROOT_URL+"registerUser1.php";
public static final String URL_updatePro = ROOT_URL+"updateUser.php";
public static final String URL_deletePro = ROOT_URL+"deleteUser.php";
public final static String urlAddress= ROOT_URL+ "listview1.php";
public final static String urlREPORT= ROOT_URL+ "reportUser.php";
public static final String UPLOAD_URL = "http://singlover.000webhostapp.com/upload.php";
// public static final String UPLOAD_URL = "http://192.168.8.101:8081/upload.php";
}