简体   繁体   English

无法从 Android 应用程序连接到 localhost API

[英]Cannot connect to localhost API from Android app

我是 Django 的新手。我创建了一个名为http:\/\/127.0.0.1:8000\/api\/update\/1\/<\/code>的 API,它可以在浏览器和 Postman 应用程序中完美运行。

但是,当我尝试从 Android 应用程序访问 API 时,它返回 NULL。

当 API 如下时,android 代码可以工作。

http://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=8190df9eb51445228e397e4185311a66

If you are testing your application from a real android device then you need to put the IP address of your PC while you are trying to connect to your Django server through APIs.如果您是在真实的 android 设备上测试您的应用程序,那么您需要在尝试通过 API 连接到 Django 服务器时输入 PC 的 IP 地址。 And yes, you need to be in the same network as well.是的,您也需要在同一个网络中。 So you need to check the following things.因此,您需要检查以下事项。

  1. Make sure that the PC (where you are running the server) and the Android device (where you are testing your application) are in the same network (connected with same Wifi network maybe).确保 PC(您运行服务器的地方)和 Android 设备(您测试应用程序的地方)在同一个网络中(可能连接到同一个 Wifi 网络)。
  2. Make sure you are connecting to the IP address of your PC where the server is running.确保您连接到运行服务器的 PC 的 IP 地址。 For example, right now, the IP address of your PC is 192.168.0.100 .例如,现在,您 PC 的 IP 地址是192.168.0.100 Then, you need to connect to this IP address and call your API like the following.然后,您需要连接到此 IP 地址并调用您的 API,如下所示。

     http://192.168.0.100:8000/api/update/1/
  3. Make sure you are accepting requests to the port 8000 in your PC.确保您正在接受对 PC 中端口8000请求。 Check your Firewall configuration if it is blocking any incoming requests to the 8000 port.检查您的防火墙配置是否阻止任何传入8000端口的请求。 If it is found blocking, then please allow an incoming request to the 8000 port using the following.如果发现阻塞,请使用以下命令允许传入请求到8000端口。

     sudo ufw allow 8000/tcp

If there is nothing which is helping you, then please check your Android code to check if the API calling is okay.如果没有任何帮助,请检查您的 Android 代码以检查 API 调用是否正常。 I would strongly recommend using Volley for API calls suggested in developers documentation.我强烈建议将 Volley 用于开发人员文档中建议的API 调用。

Last, but not the least, please check if you have necessary permission in your AndroidManifest.xml file.最后但并非最不重要的一点,请检查您的AndroidManifest.xml文件中是否有必要的权限。 You need to add the following permission to grant your application to use the internet.您需要添加以下权限才能授予您的应用程序使用互联网的权限。

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

To be able to connect your localhost (I assume you are using emulator, I can edit the answer if not)为了能够连接您的本地主机(我假设您正在使用模拟器,如果没有,我可以编辑答案)

You need to use the following url:您需要使用以下网址:

http://10.0.2.2:8000/

Please run your django development server using the following command.请使用以下命令运行您的 django 开发服务器。

python manage.py runserver 0.0.0.0:8000

Once you get it running, in a new terminal window find out the ip address of your computer in your wifi/network subnet using the following command运行后,在新的终端窗口中,使用以下命令找出您的计算机在 wifi/网络子网中的 IP 地址

Ipconfig or ifconfig (depends on your OS)

Then change the base url of your api from 127.0.0.1 to the ip you found in the above step.然后将您的 api 的基本 url 从 127.0.0.1 更改为您在上述步骤中找到的 ip。 Now connect the android phone in which your app is being tested to your same wifi or network to which the computer running django is connected.现在将正在测试您的应用程序的 android 手机连接到运行 django 的计算机所连接的同一 wifi 或网络。 Now you can request and receive response.现在您可以请求和接收响应。

127.0.0.1 is the home of your system, your android app will not be able to access that. 127.0.0.1 是您系统的主页,您的 android 应用程序将无法访问它。 You need to do it like this.你需要这样做。

1)Andoird manifest to put internet permission and network acceess 2) if using Android simulator on local machine. 1)Andoird清单放置互联网权限和网络访问2)如果在本地机器上使用Android模拟器。 Use 10.0.2.2.使用 10.0.2.2。 alias created by default for 127.0.0.1 ie losthoast.默认为 127.0.0.1 创建的别名,即 losthoast。

Its works for me它对我有用

The API isn't working in POSTMAN I tried http://127.0.0.1:8000/api/update/1/ shows an error because it requires some params to be inputted. API 在 POSTMAN 中不起作用我试过http://127.0.0.1:8000/api/update/1/显示错误,因为它需要输入一些参数。

in this case, http://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=8190df9eb51445228e397e4185311a66 the required params have been provided to the API, ie source, sortBy & apiKey在这种情况下, http ://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=8190df9eb51445228e397e4185311a66 所需的参数已提供给 API,即 source、sortBy 和 apiKey

It is possible to call local api with some minor changes.These were the steps I did to hit the local django api from my android app.可以通过一些小的更改调用本地 api。这些是我从我的 android 应用程序中访问本地 django api 所做的步骤。

  • You need to be connected to same network.您需要连接到同一网络。 Better create a hotspot from your pc or laptop and connect android with it更好地从您的 PC 或笔记本电脑创建一个热点并将其连接到 android
  • Create static IP from your system.从您的系统创建静态 IP。 If you are on VM you may require additional network adapter.如果您在 VM 上,您可能需要额外的网络适配器。 Please google it as there are plenty of videos and blogs on the same.请谷歌它,因为有很多视频和博客。
  • Host django on the created local IP.在创建的本地 IP 上托管 django。

You are then good to go with your android app.然后你就可以使用你的安卓应用程序了。

You can not access a local api outside from that local device.您无法从该本地设备访问外部的本地 api。 127.0.0.1 is the local address of its own. 127.0.0.1 是它自己的本地地址。 If you want to access the api outside from local device, replace the ip with the ip that is open to outside.如果要从本地设备访问外部的api,请将ip替换为对外开放的ip。 For example, 192.168.43.2 is a ip that is accessible from outside.例如,192.168.43.2 是一个可以从外部访问的 IP。 Update your api with the accessible ip(use ifconfig or ipconfig in terminal to obtain the ip and use that ip to access from outsid?).使用可访问的 ip 更新您的 api(在终端中使用 ifconfig 或 ipconfig 获取 ip 并使用该 ip 从外部访问?)。 Also, please make sure if you have proper permission in AndroidManifest.xml file.另外,请确保您在AndroidManifest.xml文件中有适当的权限。

For me, the problem was that my server was running on HTTP, not HTTPS.对我来说,问题是我的服务器在 HTTP 上运行,而不是 HTTPS。 Once I set up my SSL certificate, everything worked fine.设置 SSL 证书后,一切正常。 I'm using Expo and strangely the API works when in development mode through the Expo app, but after I build an APK it fails to connect to any non-HTTPS server.我正在使用 Expo,奇怪的是,API 在通过 Expo 应用程序处于开发模式时工作,但是在我构建 APK 后,它无法连接到任何非 HTTPS 服务器。

在本地主机中,您必须启动服务器并在同一系统中进行应用程序开发,然后才适合您..

There is no way of calling local APIs from android.无法从 android 调用本地 API。 Leave it.别管它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM