Java 如何从我的 android 设备访问我的本地 REST api?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41196020/
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 23:13:52  来源:igfitidea点击:

How can I access my local REST api from my android device?

javaandroidrestapilocalhost

提问by Alien

I have a spring REST api running locally on my computer. I would like to consume this api for android development.

我有一个在我的计算机上本地运行的 spring REST api。我想使用这个 api 进行 android 开发。

Here is my get request:

这是我的获取请求:

public static String sendGet(final String url) {
        StringBuilder result = new StringBuilder();
        HttpURLConnection urlConnection = null;
        try {
            String apiUrl = getAbsoluteUrl(url); // concatenate uri with base url eg: localhost:8080/ + uri
            URL requestUrl = new URL(apiUrl);
            urlConnection = (HttpURLConnection) requestUrl.openConnection();
            urlConnection.connect(); // no connection is made
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            urlConnection.disconnect();
        }
        return result.toString();
    }

I can access my api via my device's browser. However, when I use this same url within the built apk to make the request, no connection is made.

我可以通过我设备的浏览器访问我的 api。但是,当我在构建的 apk 中使用相同的 url 发出请求时,没有建立连接。

My manifest includes:

我的清单包括:

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

Side notes:

旁注:

I am connecting my device to my laptop running the rest api via usb. I am using the WLAN IPv4 address found by calling ipconfig.

我正在通过 USB 将我的设备连接到运行其余 API 的笔记本电脑。我正在使用通过调用 ipconfig 找到的 WLAN IPv4 地址。

Any tips in the right direction would be much appreciated - thanks!

任何方向正确的提示将不胜感激 - 谢谢!

Edit to include chrome browser (on android device) output from local REST api running on my laptop (A GET request to return default guest user information): enter image description here

编辑以包含我的笔记本电脑上运行的本地 REST api 的 chrome 浏览器(在 android 设备上)输出(返回默认访客用户信息的 GET 请求): 在此处输入图片说明

采纳答案by Alien

SOLVED if anyone is interested:

如果有人感兴趣,请解决:

I managed to fix this issue by extending the class my original sendGet(final String url)was in as follows HttpClientUsage extends AsyncTask<String, Void, String>more information and a tutorial can be found here: AsyncTask tutorial

我设法通过扩展我原来sendGet(final String url)所在的类来解决这个问题,HttpClientUsage extends AsyncTask<String, Void, String>更多信息和教程可以在这里找到:AsyncTask教程

I then had to configure my CORS settings on my local REST API as follows:

然后我必须在本地 REST API 上配置我的 CORS 设置,如下所示:

 cors:
        allowed-origins: "*"
        allowed-methods: GET, PUT, POST, DELETE, OPTIONS
        allowed-headers: "*"
        exposed-headers:
        allow-credentials: true
        max-age: 1800

Thank you all for your help, it is much appreciated.

谢谢大家的帮助,非常感谢。

回答by Shadow Droid

Your rest url must be something like this- http://localhost:8080/yourRest/restMethod.
Instead of localhost url connect your mobile and local machine on same network(wifi network). Get the ip address of your local machine e.g 192.168.1.X...so now your end point url for rest will be http://192.168.1.X:8080/yourRest/restMethod

您的休息网址必须是这样的- http://localhost:8080/yourRest/restMethod
而不是 localhost url 在同一网络(wifi 网络)上连接您的移动和本地机器。获取您本地机器的 IP 地址,例如 192.168.1.X...所以现在您的端点 url 将是http://192.168.1.X:8080/yourRest/restMethod

回答by Mrinmoy

Check your ip:- Steps to check ip (make sure you are connected to internet)

检查您的 ip:- 检查 ip 的步骤(确保您已连接到互联网)

  1. Open command prompt
  2. type ipconfig
  3. Ip is the highlighted one in image below
  1. 打开命令提示符
  2. 输入ipconfig
  3. Ip 是下图中突出显示的那个

enter image description here

在此处输入图片说明

Now make your url like this: http://192.168.240.2/index.html

现在让你的网址像这样:http://192.168.240.2/index.html

回答by rakesh kashyap

Let me tell you an easier way to do this. If you are using Android emulator you can use 10.0.2.2 as the IP address to connect to the host machine where your REST API is available.

让我告诉你一个更简单的方法来做到这一点。如果您使用 Android 模拟器,您可以使用 10.0.2.2 作为 IP 地址来连接到您的 REST API 可用的主机。

Similarly if you are using Genymotion which uses Oracle Virtualbox, you can use 10.0.3.2.

同样,如果您使用的是使用 Oracle Virtualbox 的 Genymotion,则可以使用 10.0.3.2。