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

后端 未结 4 1380
名媛妹妹
名媛妹妹 2021-02-13 14:03

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

Here is my get request:

public stati         


        
相关标签:
4条回答
  • 2021-02-13 14:09

    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

    0 讨论(0)
  • 2021-02-13 14:15

    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.

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

    0 讨论(0)
  • 2021-02-13 14:34

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

    1. Open command prompt
    2. type ipconfig
    3. Ip is the highlighted one in image below

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

    0 讨论(0)
  • 2021-02-13 14:36

    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

    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.

    0 讨论(0)
提交回复
热议问题