我的问题是,当我在真实的移动设备上运行应用程序时,会显示以下错误:com.android.volley.TimeoutError。然而,通过使用Android Studio模拟器可以完美地工作。由于大多数情况下这个错误是由连接问题引起的,所以我是否在使用正确的IP地址来运行我的应用程序?即:
"private static String URL_REGIST ="http://10.0.2.2:81/android_register_login/register.php";
我是应用程序开发的新手,所以任何帮助将不胜感激。下面是register.java的代码,它使用register.php脚本来执行注册。
"private static String URL_REGIST ="http://10.0.2.2:81/android_register_login/register.php";
我是应用程序开发的新手,所以任何帮助将不胜感激。下面是register.java的代码,它使用register.php脚本来执行注册。
private EditText name,email,password,c_password;
private Button btn_regist;
private ProgressBar loading;
private static String URL_REGIST ="http://10.0.2.2:81/android_register_login/register.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
loading = findViewById(R.id.loading);
name= findViewById(R.id.name);
email = findViewById(R.id.email);
password = findViewById(R.id.password);
c_password = findViewById(R.id.c_password);
btn_regist =findViewById(R.id.btn_regist);
btn_regist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Regist();
}
});
}
private void Regist(){
loading.setVisibility(View.VISIBLE);
btn_regist.setVisibility(View.GONE);
final String name = this.name.getText().toString().trim();
final String email = this.email.getText().toString().trim();
final String password = this.password.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_REGIST,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success");
if(success.equals("1")){
Toast.makeText(RegisterActivity.this, "Account Created",Toast.LENGTH_SHORT).show();
loading.setVisibility(View.GONE);
}
}catch (JSONException e) {
e.printStackTrace();
Toast.makeText(RegisterActivity.this, "Register Error!" + e.toString(),Toast.LENGTH_SHORT).show();
loading.setVisibility(View.GONE);
btn_regist.setVisibility(View.VISIBLE);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(RegisterActivity.this, "Register Error!" + error.toString(),Toast.LENGTH_SHORT).show();
loading.setVisibility(View.GONE);
btn_regist.setVisibility(View.VISIBLE);
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("name", name);
params.put("email", email);
params.put("password", password);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}