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