为什么我的 http post 请求 android 凌空抛出错误(本地主机)?

Why my http post request with android volley throws error (localhost)?

我正在做一个 android 工作室项目,我在尝试使用 volley 库发出 post 请求时遇到了一些问题。我已经用 postman 测试了我的 API,一切正常,所以问题出在客户端。

我已经在我的 android 清单中添加了互联网权限:

<uses-permission android:name="android.permission.INTERNET"/>

这是我的代码,我创建了一个名为 "login" 的函数,它接收两个参数,它们是我想通过 post 方法发送的数据:

     private void login(final String email, final String password){
        final String url = "http://192.168.100.2:8000/login_facebook_app";

        RequestQueue requestQueue = Volley.newRequestQueue(this);

        StringRequest postRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Toast.makeText(getBaseContext(), response, Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getBaseContext(), error.toString(), Toast.LENGTH_SHORT).show();
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String>  params = new HashMap<String, String>();
                params.put("email", email);
                params.put("password", password);

                return params;
            }
        };

        postRequest.setRetryPolicy(new DefaultRetryPolicy(
                15000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        requestQueue.add(postRequest);
    }

我在单击事件登录按钮上调用该函数,但是当我单击该按钮时我得到

com.android.volley.timeoutError

我已经阅读了很多解决方案,我发现超时问题是由于本地主机中 API 为 运行 时的防火墙问题。 但是当我禁用防火墙时会抛出另一个错误:

com.android.volley.NoConnectionError:java.net.ConnectionException: failed to connect to /192.168.100.2(port 8000) after 2500ms:isConnected failed: ECONNREFUSED(Connection refused)

我已经尝试了很多这里的解决方案,但我的应用程序无法运行。请帮助我 xD

我找到了连接问题的解决方案;我一直在尝试通过我的手机和我的计算机都连接的 IP 从我的手机访问 url,这是我计算机上本地主机中的 运行,尽管这对我因为请求在到达服务器之前就死了,所以我尝试了另一种方式,这是从您的设备访问计算机中本地主机的 url 运行 的答案:

首先,请确保您的服务器是运行。

然后在应用程序标签的 AndroidManifest 文件中将 usesCleartextTraffic 设置为 true。

    <application
        .....
        android:usesCleartextTraffic="true"
     >
            ....
        </activity>

然后在 url 而不是“http://192.168.100.2:8000/login_facebook_app”中使用这个“http://10.0.2.2:8000/login_facebook_app”

现在您的新密码是:

     private void login(final String email, final String password){
        final String url = "http://10.0.2.2:8000/login_facebook_app";

        RequestQueue requestQueue = Volley.newRequestQueue(this);

        StringRequest postRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Toast.makeText(getBaseContext(), response, Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getBaseContext(), error.toString(), Toast.LENGTH_SHORT).show();
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String>  params = new HashMap<String, String>();
                params.put("email", email);
                params.put("password", password);

                return params;
            }
        };

        postRequest.setRetryPolicy(new DefaultRetryPolicy(
                15000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));