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