0

I would like to make a RESTful service available from an Android app via HTTP requests. For development I set up a Tomcat server in eclipse and an SQLite database. HTTP requests to localhost:80 from a simple Java client application work just fine: I can verify that new entries get added to the database.

Now I would like to make these HTTP requests from an Android app and I can not figure out why it won't work when I test it on my device. As the Apache HTTPClient is deprecated, I tried it with HttpURLConnection first:

private void proceedSignup() {
    String domain = "http://10.0.2.2";
    String port = ":80";
    String urlString = domain + port + "/AppServer/register/doregister?login=android&firstName=vorn&secondName=nach";

    Log.d("DEBUG", "HTTP Request");
    HttpURLConnection urlConnection;
    try {
        URL url = new URL(urlString);
        urlConnection = (HttpURLConnection) url.openConnection();
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        String response = readStream(in);
        Log.d("DEBUG", response);
    } catch (Exception e ) {
        System.out.println(e.getMessage());
    }

    this.finish();
}

I also added this required permission to the manifest file:

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

The response string is always null, although a JSON structure should be returned in case of success. It's not surprising that the requests don't arrive at the server via 192.168.1.1 or 127.0.0.1, however, it should work with the alias 10.0.0.2 (see here).

I also tried to use the legacy Apache HTTPClient, though it shouldn't make any difference which library I use. Then I used ngrok to create a tunnel to my localhost at port 80. Didn't help either.

Might this have something to do with the server configuration, as described in this post? My Tomcat server configuration file doesn't contain any specific IP, though...

Community
  • 1
  • 1
stefanS
  • 312
  • 4
  • 17
  • "when I test it on my device" -- are you testing on a device? The documentation that you linked to is for an emulator. Emulators are not devices. – CommonsWare Jan 04 '16 at 13:33
  • Are you using emulator or phone ? – R_K Jan 04 '16 at 13:36
  • Yes, I am testing on my phone with USB debugging. Unfortunately I could not get the Android emulator to work on my computer. I didn't realize until now that the linked overview table only applies to the emulator. – stefanS Jan 04 '16 at 13:43
  • If you use emulator it works with 10.0.2.2 ip! – Mohamadamin Oct 15 '21 at 11:35

2 Answers2

0

I think this problem occur because of Http Connection is not successfully establish with you server. To set up your local server successfully you need to use intranet instead of intranet. you need to establish intranet connection between your device and your local server. this will help you and use your IP address to establish connection with your local server.

Umesh Saraswat
  • 560
  • 1
  • 8
  • 22
0

In your code you have:

String domain = "http://10.0.2.2";

But in your description you mention

it should work with the alias 10.0.0.2

Are you just hitting the wrong IP?

TJ_
  • 328
  • 2
  • 13