【问题标题】:Cannot access Localhost in android emulator无法在android模拟器中访问Localhost
【发布时间】:2018-11-10 07:01:43
【问题描述】:

我有一个 django 服务器,但应用程序在 android 模拟器中崩溃,为什么?

这是代码:

final TextView t = findViewById(R.id.textView3);
    RequestQueue queue = Volley.newRequestQueue(this);
    String url = "http://10.0.2.2:8000/";
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    // Display the first 500 characters of the response string.
                    t.setText("Response is: "+ response.substring(0,500));
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            t.setText("That didn't work!");
        }
    });

【问题讨论】:

  • 请张贴logcat。你需要告诉异常是被抛出而不是被捕获。以及哪个语句导致了崩溃。

标签: android django android-studio localhost android-volley


【解决方案1】:

如果服务器在 localhost 上运行,而 app 在模拟器上运行,那么您必须在 app 中将另一个 IP 作为 BaseUrl。

步骤: -> 打开终端,然后输入 ifconfig(在 mac os 上) -> 复制en0中显示的inet IP地址,并在app中设置为BaseUrl

【讨论】:

    【解决方案2】:

    可能重复How do you connect localhost in the Android emulator?

    如果Genymotion 则尝试 10.0.3.2

    【讨论】:

      【解决方案3】:

      使用您计算机的实际 IP 地址!用你的系统实际IP替换10.0.2.2,检查java.net.ConnectException: failed to connect to /10.0.0.2 (port 80): connect failed: ETIMEDOUT (Connection timed out)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-27
        • 2013-12-13
        • 2018-05-18
        • 2017-12-11
        • 1970-01-01
        • 2020-05-28
        • 2012-01-08
        相关资源
        最近更新 更多