android - 从 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());
}
}
);
解决方案
使用 ifconfig 或 ipconfig 查找您的本地 IP,并使用该 IP 调用您的服务器
推荐阅读
- php - Laravel 广播认证不使用标准用户认证
- php - 从文件中读取环境变量
- amazon-web-services - 处理不需要/旧/陈旧 AWS Athena 分区的正确方法
- powershell - 如何在文件夹中查找字符串并使用 PowerShell 返回值?
- c# - 有没有办法从 SQL 语句中获取列名?
- javascript - 为什么画布上的矩形会移位?
- maven - 如何将 Cytoscape 画布集成到 JavaFX Maven 应用程序中?
- paypal - Silvestripe 4.4 无法安装omnipay/paypal
- android - 在每台 Android 设备上显示相同大小的文本
- android - 使用 Mockk 模拟语言环境