[英]My app is working on my android emulator however it is not working on my real mobile device
My problem is when I run my application on a real mobile device the following error is displayed: com.android.volley.TimeoutError".However, it works perfectly by using the emulator of Android studio. Due to the fact that must of the time this error is caused by connectivity issues, am I using the correect ip for my application? ie 我的问题是,当我在真实的移动设备上运行我的应用程序时,显示以下错误:com.android.volley.TimeoutError“。但是,它通过使用Android工作室的模拟器完美地工作。由于必须有时间这个错误是由连接问题引起的,我是否在我的应用程序中使用了correect ip?ie
"private static String URL_REGIST ="http://10.0.2.2:81/android_register_login/register.php"; " “private static String URL_REGIST =”http://10.0.2.2:81/android_register_login/register.php“;”
I am new to application development so any help would greatly be appreciated. 我是应用程序开发的新手,所以任何帮助都会非常感激。 Below is the code for register.java which makes use of register.php script which is used to perform registration
下面是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);
}
} }
That's the wrong IP address. 这是错误的IP地址。 10.0.2.2 is only going to work on emulators, because that's the IP of the private network between the host and the emulator.
10.0.2.2仅适用于仿真器,因为它是主机和仿真器之间专用网络的IP。 For wifi, you need to use the real IP address and make sure the wifi network is set up to allow incoming connections on that IP/port.
对于wifi,您需要使用真实IP地址并确保设置wifi网络以允许该IP /端口上的传入连接。 For cellular, you need to make sure that's the case all the way up to your ISP
对于蜂窝网络,您需要确保一直到您的ISP的情况
If you are testing in Android 9 Pie then you will have to set a networkSecurityConfig in your Manifest application
tag to allow all HTTP and HTTPS network connection. 如果您在Android 9 Pie中进行测试,则必须在Manifest
application
标记中设置networkSecurityConfig以允许所有HTTP和HTTPS网络连接。 Also, check if you have provided INTERNET permission. 另外,检查您是否提供了INTERNET权限。
<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
<application android:networkSecurityConfig="@xml/network_security_config">
</application>
</manifest>
Then in your xml folder you now have to create a file named network_security_config just like the way you have named it in the Manifest and from there the content of your file should be like this to enable all requests without encryptions: 然后在你的xml文件夹中,你现在必须创建一个名为network_security_config的文件,就像你在Manifest中命名它一样,从那里你的文件内容应该是这样的,以启用所有没有加密的请求:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
Most probably the error is due to a connectivity issue. 最可能的错误是由于连接问题。 https://10.0.2.2 must be your localhost IP address.
https://10.0.2.2必须是您的本地主机IP地址。 The application running on your Android device can not connect to a localhost server.
在Android设备上运行的应用程序无法连接到本地主机服务器。
Solutions : 方案:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.