无法从 Android 应用程序连接到 localhost API [英] Cannot connect to localhost API from Android app

查看:28
本文介绍了无法从 Android 应用程序连接到 localhost API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

当 API 如下时,android 代码工作.

The android code works when the API is the following.

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

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

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.

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

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

I am attaching the code where API call is made.

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.

推荐答案

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

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.

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

  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/

  • 确保您正在接受对 PC 中端口 8000 的请求.检查您的防火墙配置是否阻止了对 8000 端口的任何传入请求.如果发现阻塞,请使用以下命令允许传入请求到 8000 端口.

  • Make sure you are accepting requests to the port 8000 in your PC. Check your Firewall configuration if it is blocking any incoming requests to the 8000 port. If it is found blocking, then please allow an incoming request to the 8000 port using the following.

    sudo ufw allow 8000/tcp
    

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

    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 developers documentation.

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

    Last, but not the least, please check if you have necessary permission in your AndroidManifest.xml file. 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" />
    

    这篇关于无法从 Android 应用程序连接到 localhost API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆