Androidアプリにホストへのルートが存在するかどうかを確認する必要があります。これが私のコードです:
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();
}
ほとんどのデバイスで完全に機能しますが、それは本当に奇妙なことです。NexusSEuropeでは機能しません。私はいくつかのNexusを試しましたが、いつもその問題が発生しました。問題はensureRouteToHost
、を呼び出すときのメソッドにありますconnMgr.requestRouteToHost(apnTypes[i], inetAddr)
。私が何を入れても、常にfalseを返します。私の計画は192.168.10.200
、MMSアプリのIPが到達可能かどうかを確認することです。これは、stackoverflow.com(69.59.197.21またはintとして1161544981)などのパブリックIPでも機能しません。
それで、これが一部のデバイスで機能しない理由がわかりますか?私のスレッドを読んでくれてありがとう。