首页 > 解决方案 > 从 android studio 连接 django devserver 失败

问题描述

我正在尝试使用 volley 从本地机器上的 android studio 连接到 django rest 框架服务器。我尝试在命令行上使用 0.0.0.0:8000 运行服务器,并在 settings.py 中添加了 ALLOWED_HOST=[*]。

android studio中的错误消息

com.android.volley.NoConnectionError: java.net.ConnectException: Failed to connect to /0.0.0.0:8000

下面是安卓客户端代码

PS:- 包括所有导入库。

 String URL= "http://0.0.0.0:8000/api/userdata/";
 RequestQueue requestQueue = Volley.newRequestQueue(this);
 JsonObjectRequest objectRequest = new JsonObjectRequest(
            Request.Method.GET,
            URL,
            null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.e("Response:",response.toString());

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("Response:",error.toString());
                }
            }
    );

标签: androiddjangodjango-rest-framework

解决方案


使用 ifconfig 或 ipconfig 查找您的本地 IP,并使用该 IP 调用您的服务器


推荐阅读