我需要检查我的Android应用是否存在到主机的路由。这里是我的代码:RequestRouteToHost不起作用
private void ensureRouteToHost(String proxyAddr) throws IOException
{
ConnectivityManager connMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
int inetAddr;
inetAddr = lookupHost(proxyAddr); // Return -938825536 for IP 192.168.10.200
Log.d(TAG, "host for proxy is " + inetAddr);
if (inetAddr == -1)
{
Log.e(TAG, "cannot establish route for " + proxyAddr + ": unkown host");
throw new IOException("Cannot establish route for " + proxyAddr + ": Unknown host");
}
else
{
int[] apnTypes = new int[] {ConnectivityManager.TYPE_MOBILE, ConnectivityManager.TYPE_MOBILE_MMS, ConnectivityManager.TYPE_MOBILE_DUN, ConnectivityManager.TYPE_MOBILE_HIPRI, ConnectivityManager.TYPE_MOBILE_SUPL};
for (int i=0; i<apnTypes.length; i++)
{
if (connMgr.requestRouteToHost(apnTypes[i], inetAddr))
{
Log.d(TAG, "route to host requested");
return;
}
}
Log.e(TAG, "unable to request route to host");
throw new IOException("Cannot establish route to proxy " + inetAddr);
}
}
public static int lookupHost(String hostname)
{
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 Long.valueOf(Long.parseLong(result, 16)).intValue();
}
它完美对大多数设备,但,这是很奇怪,它不会在Nexus S的欧洲工作。我试了几次Nexus,我总是遇到这个问题。 当我拨打connMgr.requestRouteToHost(apnTypes[i], inetAddr)
时,问题位于ensureRouteToHost
方法中。无论我输入什么,它总是返回假。我的计划是检查IP 192.168.10.200
是否可用于我的MMS应用程序。 这不适用于公共IP,如stackoverflow.com(作为整数69.59.197.21或1161544981)。
那么,你有一个想法,为什么这不适用于某些设备? 感谢您阅读我的话题。
您的代码在Galaxy Nexus GSM(CyanogenMod 10)上运行,并带有TYPE_MOBILE(显然只有在启用移动数据连接时) – bwt
感谢您的反馈。它适用于Xperia T 4.0.3,Desire S 4.0.4,Galaxy S3 4.1,Huawei Ascend P1 4.0.3等等。这些问题出现在Nexus S欧洲版本4.0.3下。数据在所有设备上启用。更换SIM卡并不能解决问题。 – Manitoba