IP failure in android

I get access to the server for web service calls. When I develop on the same network as the server, I can access the web service by its internal IP address, but not by its external IP address. However, if I'm offline, I can only access it by the external IP address. What is the best way to try one of the IP addresses and then return to the other?

Here is an example of my code for accessing only one or the other:

protected String retrieve() { Log.v(TAG, "retrieving data from url: " + getURL()); HttpPost request = new HttpPost(getURL()); try { StringEntity body = new StringEntity(getBody()); body.setContentType(APPLICATION_XML_CONTENT_TYPE); request.setEntity(body); HttpConnectionParams.setConnectionTimeout(client.getParams(), CONNECTION_TIMEOUT); HttpConnectionParams.setSoTimeout(client.getParams(), SOCKET_TIMEOUT); HttpResponse response = client.execute(request); final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { Log.e(TAG, "the URL " + getURL() + " returned the status code: " + statusCode + "."); return null; } HttpEntity getResponseEntity = response.getEntity(); if (getResponseEntity != null) { return EntityUtils.toString(getResponseEntity); } } catch (IOException e) { Log.e(TAG, "error retrieving data.", e); request.abort(); } return null; } /* * @return the URL which should be called. */ protected String getURL() { return INTERNAL_SERVER_URL + WEB_APP_PATH; } 
+7
source share
8 answers

Look at your Android's own IP address. You can get it as indicated here .

Then you can decide:

  • if you are on the subnet of your office (for example, 192.168.0.0/16) - use the internal address
  • if you are on a different subnet - use an external address
+4
source

Based on a very good comment on charisma, I would just use a static boolean to select an IP address and thus avoid ping the wrong IP address every time:

 public static final Boolean IS_DEBUG = true; // or false // all your code here if (DEBUG) ip = xxx.xxx.xxx.xxx; else ip = yyy.yyy.yyy.yyy; 
+3
source

This is not exactly what you can easily fix in software. The correct answer, in my opinion, is to fix the filters / configurations that direct traffic to your internal web server or by setting the DNS correctly to return the correct IP address depending on where you are located (inside or outside the network). More details can be found here:

Access to an internal network resource using an external IP address

http://www.astaro.org/astaro-gateway-products/network-security-firewall-nat-qos-ips-more/6704-cant-acces-internal-webserver-via-external-ip.html

and according to Googling something like "the external IP address does not work on the internal network"

+3
source

You can put the retry code in a catch clause for an IOException

 protected String retrieve(String url) { Log.v(TAG, "retrieving data from url: " + url); HttpPost request = new HttpPost(url); try { StringEntity body = new StringEntity(getBody()); body.setContentType(APPLICATION_XML_CONTENT_TYPE); request.setEntity(body); HttpConnectionParams.setConnectionTimeout(client.getParams(), CONNECTION_TIMEOUT); HttpConnectionParams.setSoTimeout(client.getParams(), SOCKET_TIMEOUT); HttpResponse response = client.execute(request); final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { Log.e(TAG, "the URL " + getURL() + " returned the status code: " + statusCode + "."); return null; } HttpEntity getResponseEntity = response.getEntity(); if (getResponseEntity != null) { return EntityUtils.toString(getResponseEntity); } } catch (IOException e) { if(url.equals(EXTERNAL_URL){ return retrieve(INTERNAL_URL); } Log.e(TAG, "error retrieving data.", e); request.abort(); } return null; } 

Note. Like most people, this is probably not a great solution for a production release, but for testing it will probably work just fine.

+3
source

You can modify the retrieve () call to accept the host as a parameter. At startup, try ping for each of the possible hosts. Make a simple request to get something that returns very quickly (like a test page). Go through every possible host you want to try. After you find one that works, save this host and use it for all future calls.

+1
source

Fourth, you should be able to handle the case at runtime. I would highly recommend vs various builds. For example: try external, if not execute internal.

Some heuristics:
An internal IP address means the network is the same. Thus, you can check it if it is the same, try the internal address of the 1st, otherwise the external one takes precedence. Later, save the mapping of the local IP address to a successfully connected one and view it to change the priority.

The resolution itself can be done by querying the root of the "/" server with a timeout (you can use 2 different threads to transfer the task at the same time, if you so wish).

Moreover, if you have access to a router / firewall, this can be done for external address recognition and proper processing. That way you can end up with an external address that works correctly.

+1
source

I would put this environmental information in a properties file (or some configuration file anyway). All you need is a single line file (obviously, you will change the IP address to what you need):

 serverUrl=192.168.1.1 

Java already has a built-in function for reading and writing such files (see link above). You can also save database connection information, etc. In this file. All that is true for the environment.

In your code, it looks like you are using constants to store the server url. I would not suggest this. What happens when changing the server url? You will need to modify and recompile your code. However, no code changes are required with the configuration file.

+1
source
 import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.methods.GetMethod; 

and

 HttpClient client = new HttpClient(); HttpMethod method = new GetMethod("http://www.myinnersite.com"); int responseCode = client.executeMethod(method); if (responseCode != 200) { method = new GetMethod("http://www.myoutersite.com"); } 

etc...

0
source

All Articles