2

I want to use the following code for my application:

InetAddress inetAddress;
try {
        inetAddress = InetAddress.getByName(hostname);
} catch (UnknownHostException e) {
        return -1;
}

It works well on most of the devices I've tested but on the Nexus S Europe and a Huawei device, it throws an exception.

cannot establish route for 192.168.010.200: unkown host

I've already tried to fix it using the following code in my Application class, but without success:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(policy);

I've also tried to use AsyncTask but I got the same error. Here is the code I used:

private int mInetAddr = -1;
private boolean mInetAck = false; // Acknowledgement

private class AsyncInetAddress extends AsyncTask<String, Void, Void>
{
    @Override
    protected Void doInBackground(String... hostname)
    {
        InetAddress inetAddress;
        try
        {
            inetAddress = InetAddress.getByName(hostname[0]);
        }
        catch (UnknownHostException e)
        {
            mInetAddr = -1;
            return null;
        }

        byte[] addrBytes;
        int addr;
        addrBytes = inetAddress.getAddress();
        addr = ((addrBytes[3] & 0xff) << 24)
                | ((addrBytes[2] & 0xff) << 16)
                | ((addrBytes[1] & 0xff) << 8)
                | (addrBytes[0] & 0xff);
        mInetAddr = addr;
        return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {
        mInetAck = true; // Acknowledgement
    }
}

Do you have any idea on how I could fix that ?

Thanks.

Edit: I've tried on some other devices, problem looks to be present only on version 4.0.* . Works great on 2.* , 3.* and 4.1+.

Now the problem is located at that line:

 if (!connMgr.requestRouteToHost(2, inetAddr))

Where inetAddr = -938825536. The first param is the MMS type. The condition is always true under a device running 4.0.3 or 4.0.4.

Manitoba
  • 8,522
  • 11
  • 60
  • 122

4 Answers4

1

At first, what's the specific error that you get?
It is possible that it isn't the problem of the device, but the Android version your are running.

and try to change this:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(policy);  

to:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
 StrictMode.setThreadPolicy(policy);
Bigflow
  • 3,616
  • 5
  • 29
  • 52
0

Try to use AsyncTask to make your request.

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

Gabriel Augusto
  • 1,002
  • 11
  • 18
  • Thanks but why does it work on some devices and not on others ? This problem is really weird. – Manitoba Jan 15 '13 at 16:02
  • 1
    it depends on the Android version,recents versions don't let UI Thread do network stuff. – VinceFR Jan 15 '13 at 16:06
  • 1
    As Vince sayed, new Android versions do not let you make network requests and works in the MainThread (like the activity or Service). AsyncTask is the best way do to that. If works, mark the question to sinalize. – Gabriel Augusto Jan 15 '13 at 18:09
  • Android implemented this from 3.0 and higher. – Bigflow Jan 16 '13 at 07:51
  • I'm going to test a piece of code that implements AsyncTask. I'll give you the results asap. – Manitoba Jan 16 '13 at 08:06
  • I've tried to use it but it gives me the same error. Check my first post to see the code I used. Any other idea ? – Manitoba Jan 16 '13 at 09:01
  • Exception returned is the same: `Unable to resolve host "192.168.010.200": No address associated with hostname` – Manitoba Jan 16 '13 at 09:09
  • I think that is a problem with your code. Check in your AsyncTask if you are receiving the certain parameter in `hostname[0]` – Gabriel Augusto Jan 16 '13 at 15:10
  • Log your parameter `String hostName = hostname[0]; Log.d(TAG, hostName);` check it – Gabriel Augusto Jan 16 '13 at 15:27
  • @Vodemki ? Give us feedback – Gabriel Augusto Jan 21 '13 at 15:24
  • I've been able to make it work on a 4.0.3 device setting up a new APN. I've also changed the `mInetAddr` value with a homemade converter whereas the use of `inetAddress.getAddress()`. I'm still trying to make it work on all devices. Without success for the moment. – Manitoba Jan 22 '13 at 08:14
0

The solution I've found so far is the following:

public static int lookupHost(String hostname) {
    // Hostname is to be `XXX.XXX.XXX.XXX` or `XXX.XXX.XXX.XXX:XXXX`
    hostname = hostname.substring(0, hostname.indexOf(":") > 0 ? hostname.indexOf(":") : hostname.length());
    String result = "";
    String[] array = hostname.split("\\.");
    if (array.length != 4) return -1;

    int[] hexArray = new int[] {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
    hexArray[0] = Integer.parseInt(array[0]) / 16;
    hexArray[1] = Integer.parseInt(array[0]) % 16;
    hexArray[2] = Integer.parseInt(array[1]) / 16;
    hexArray[3] = Integer.parseInt(array[1]) % 16;
    hexArray[4] = Integer.parseInt(array[2]) / 16;
    hexArray[5] = Integer.parseInt(array[2]) % 16;
    hexArray[6] = Integer.parseInt(array[3]) / 16;
    hexArray[7] = Integer.parseInt(array[3]) % 16;

    for (int i=0; i<8; i++)
    {
        result += Integer.toHexString( hexArray[i] );
    }

    return (new Long( Long.parseLong(result, 16) )).intValue();
}

It looks to be working on most devices I've tested.

Manitoba
  • 8,522
  • 11
  • 60
  • 122
0

Problem solved. It can't find the route to IP while the wifi is enabled. Simplest way is to disable wifi, do your stuff and then enable wifi.

Here is the code I used:

// Disable wifi if it's active
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled())
{
      mWasWifiActive = true;
      wifiManager.setWifiEnabled(false);
      Log.e(TAG, "Wifi was enabled, now Off.");
}

// Do stuff here

// Re-enable wifi if it was active before routing
if (mWasWifiActive)
{
       WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
       wifiManager.setWifiEnabled(true);
       Log.e(TAG, "Wifi is back online.");
}
Manitoba
  • 8,522
  • 11
  • 60
  • 122