简体   繁体   English

我的应用程序正在我的Android模拟器上工作,但它不能在我的真实移动设备上工作

[英]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 : 方案:

  • You can use ngrok. 你可以使用ngrok。 Basically, it creates a tunnel to your localhost through which you can connect to your localhost server. 基本上,它会创建一个到本地主机的隧道,您可以通过该隧道连接到本地主机服务器。 I have personally used this with my Flask API hosted on localhost 我亲自将这个用于我在localhost上托管的Flask API
  • You can also host your server on AWS (Amazon Web Services), Hostinger or on other cloud services. 您还可以在AWS(Amazon Web Services),Hostinger或其他云服务上托管您的服务器。 I have used Hostinger for hosting my API in PHP. 我用Hostinger在PHP中托管我的API。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取请求在 android 设备上不起作用,但它在我的电脑上运行 - get request is not working on android device however it's working on my pc [linphone-android]正在使用所有模拟器,但停止了在我的真实设备上工作4.4.2 - [linphone-android]working all emulator but stopped working my real device 4.4.2 我的Android应用程序在模拟器上运行,但无法在我的Android设备上运行 - My Android application runs on emulator but not working on my android device 当我的android应用程序在android设备/模拟器上运行时Jsoup无法正常工作 - Jsoup is not working when my android application is running on android device/emulator 在仿真器上工作但不在真正的Android设备上 - Working on Emulator but not on the real Android device 我的应用程序在模拟器上工作(Pixel 2 Api 25)但不在安卓设备上(Andriod 版本[9、8 等]) - My App Is Working On Emulator (Pixel 2 Api 25)but not on android device(Andriod Version[9 , 8 etc]) 我的 android 应用程序在我的虚拟设备上运行,但不在我的物理 android 设备上 - My android app is working on my virtual device, but not on my physical android device 应用程序在模拟器上运行,但无法在真实设备上运行 - App running on emulator but not working on real device 为什么我的应用程序无法在模拟器中运行? - Why's my app not working in the emulator? 为什么我的 android 工作室模拟器不工作? - Why my android studio emulator is not working?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM