如何从我的 android 设备访问我的本地 REST api? [英] How can I access my local REST api from my android device?

查看:53
本文介绍了如何从我的 android 设备访问我的本地 REST api?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我的获取请求:

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();
    }

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

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.

我的清单包括:

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

附注:

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

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.

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

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

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

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):

推荐答案

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

SOLVED if anyone is interested:

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

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

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

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

 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.

这篇关于如何从我的 android 设备访问我的本地 REST api?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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