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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51889837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:17:23  来源:igfitidea点击:

Cannot connect to localhost API from Android app

javaandroiddjangoapilocalhost

提问by Mazharul Islam

I am new to Django. I have created an API named http://127.0.0.1:8000/api/update/1/and it works perfectly in the browser and on the Postman app.

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

However, when I am trying to access the API from the Android app it is returning NULL.

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

The android code works when the API is the following.

当 API 如下时,android 代码有效。

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

However, it does not work when the API is the following, even if the following API works just fine from my browser running in the same PC and from the Postman application.

但是,当 API 如下时它不起作用,即使以下 API 在我在同一台 PC 上运行的浏览器和 Postman 应用程序中工作得很好。

http://127.0.0.1:8000/api/update/1/

I am attaching the code where API call is made.

我附上了进行 API 调用的代码。

protected String doInBackground(String... args) {
    String xml = "";
    String api = "http://127.0.0.1:8000/api/update/1/"; // returns NULL works in Postman app and in the browser
    // String api = "http://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=8190df9eb51445228e397e4185311a66"; // works in all places
    String urlParameters = "";
    xml = Function.excuteGet(api, urlParameters);
    return  xml;
}

Can anyone help me with this? Thanks in advance.

谁能帮我这个?提前致谢。

采纳答案by Reaz Murshed

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. And yes, you need to be in the same network as well. So you need to check the following things.

如果您是在真实的 android 设备上测试您的应用程序,那么您需要在尝试通过 API 连接到 Django 服务器时输入 PC 的 IP 地址。是的,您也需要在同一个网络中。因此,您需要检查以下事项。

  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).
  2. Make sure you are connecting to the IP address of your PC where the server is running. For example, right now, the IP address of your PC is 192.168.0.100. Then, you need to connect to this IP address and call your API like the following.

    http://192.168.0.100:8000/api/update/1/
    
  3. Make sure you are accepting requests to the port 8000in your PC. Check your Firewall configuration if it is blocking any incoming requests to the 8000port. If it is found blocking, then please allow an incoming request to the 8000port using the following.

    sudo ufw allow 8000/tcp
    
  1. 确保 PC(您运行服务器的地方)和 Android 设备(您测试应用程序的地方)在同一个网络中(可能连接到同一个 Wifi 网络)。
  2. 确保您连接到运行服务器的 PC 的 IP 地址。例如,现在,您 PC 的 IP 地址是192.168.0.100. 然后,您需要连接到此 IP 地址并调用您的 API,如下所示。

    http://192.168.0.100:8000/api/update/1/
    
  3. 确保您正在接受对8000PC端口的请求。检查您的防火墙配置是否阻止了对8000端口的任何传入请求。如果发现阻塞,请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. I would strongly recommend using Volley for API calls suggested in developersdocumentation.

如果没有任何帮助,请检查您的 Android 代码以检查 API 调用是否正常。我强烈建议将 Volley 用于开发人员文档中建议的API 调用。

Last, but not the least, please check if you have necessary permission in your AndroidManifest.xmlfile. You need to add the following permission to grant your application to use the internet.

最后但并非最不重要的一点,请检查您的AndroidManifest.xml文件是否具有必要的权限。您需要添加以下权限才能授予您的应用程序使用互联网的权限。

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

回答by Anjani Mittal

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=8190df9eb51445228e397e4185311a66the 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

回答by Faizan Mubasher

There is no way of calling local APIs from android. Leave it.

无法从 android 调用本地 API。别管它。

回答by Prudhvi Bsecure

In local host You must start the server and also do app development in same system then only it works for you..

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

回答by Aman trigunait

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

You are then good to go with your android app.

然后你就可以使用你的安卓应用程序了。

回答by Arun T

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. 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. Now you can request and receive response.

然后将您的 api 的基本 url 从 127.0.0.1 更改为您在上述步骤中找到的 ip。现在将正在测试您的应用程序的 android 手机连接到运行 django 的计算机所连接的同一 wifi 或网络。现在您可以请求和接收响应。

127.0.0.1 is the home of your system, your android app will not be able to access that. You need to do it like this.

127.0.0.1 是您系统的主目录,您的 android 应用程序将无法访问它。你需要这样做。

回答by mahfuz195

You can not access a local api outside from that local device. 127.0.0.1 is the local address of its own. If you want to access the api outside from local device, replace the ip with the ip that is open to outside. For example, 192.168.43.2 is a ip that is accessible from outside. 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?). Also, please make sure if you have proper permission in AndroidManifest.xmlfile.

您无法从该本地设备访问外部的本地 api。127.0.0.1 是它自己的本地地址。如果要从本地设备访问外部的api,请将ip替换为对外开放的ip。例如,192.168.43.2 是一个可以从外部访问的 IP。使用可访问的 ip 更新您的 api(在终端中使用 ifconfig 或 ipconfig 获取 ip 并使用该 ip 从外部访问?)。另外,请确保您在AndroidManifest.xml文件中有适当的权限。

回答by aomerk

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/