Question

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.

Was it helpful?

Solution 4

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.");
}

OTHER TIPS

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);

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top