2013-01-24 19 views
1

我需要检查我的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)。

那么,你有一个想法,为什么这不适用于某些设备? 感谢您阅读我的话题。

+0

您的代码在Galaxy Nexus GSM(CyanogenMod 10)上运行,并带有TYPE_MOBILE(显然只有在启用移动数据连接时) – bwt

+0

感谢您的反馈。它适用于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

回答

-1

问题解决了。 它无法找到与wifi上的IP路由。最简单的方法是禁用wifi,做你的东西,然后启用wifi。

这里是我使用的代码:

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

你需要拿那些接口,例如首先是HIPRI。方法是described here

但是我发现虽然这会使两个接口都出现,并且requestRouteToHost返回true(至少,一旦网络实际上运行),但路由仍然不起作用。

这是在许多不同的手机上测试。

请让我知道,如果你成功与此。