내 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 Europe에서 작동하지 않습니다. 나는 Nexus 몇 가지를 시험해 보았는데 항상 문제가 발생했습니다. connMgr.requestRouteToHost(apnTypes[i], inetAddr)
을 호출하면이 문제가 ensureRouteToHost
메서드에 있습니다. 내가 입력 한 내용은 항상 false를 반환합니다. MMS 앱에 IP 192.168.10.200
에 연결할 수 있는지 확인해야합니다. 이것은 공용 IP (stackoverflow.com 같은) (69.59.197.21 또는 1161544981 int로)는 작동하지 않습니다.
왜 일부 기기에서는 작동하지 않는 이유가 있습니까? 내 스레드를 읽어 주셔서 감사합니다.
TYPE_MOBILE이있는 Galaxy Nexus GSM (CyanogenMod 10)에서 코드 작업 (모바일 데이터 연결이 설정된 경우에만 가능) – bwt
의견에 감사드립니다. Xperia T 4.0.3, Desire S 4.0.4, Galaxy S3 4.1, Huawei Ascend P1 4.0.3 등에서도 작동합니다. 문제는 Nexus S 4.0u 버전에서 나타납니다. 데이터는 모든 기기에서 사용 설정됩니다. SIM 카드를 변경해도 문제가 해결되지 않습니다. – Manitoba