简体   繁体   English

某些设备上的Android Issue

[英]Android Issue on some devices

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. 它在我测试过的大多数设备上都能很好地工作,但在Nexus S Europe和华为设备上,它会抛出异常。

cannot establish route for 192.168.010.200: unkown host 无法为192.168.010.200建立路由:主机未知

I've already tried to fix it using the following code in my Application class, but without success: 我已经尝试在我的Application类中使用以下代码修复它,但没有成功:

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. 我也尝试过使用AsyncTask,但是遇到了同样的错误。 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.* . 编辑:我在其他一些设备上尝试过,问题似乎仅在版本4.0。*上存在。 Works great on 2.* , 3.* and 4.1+. 适用于2. *,3. *和4.1+。

Now the problem is located at that line: 现在问题就在那条线上:

 if (!connMgr.requestRouteToHost(2, inetAddr))

Where inetAddr = -938825536 . 其中inetAddr = -938825536 The first param is the MMS type. 第一个参数是MMS类型。 The condition is always true under a device running 4.0.3 or 4.0.4. 在运行4.0.3或4.0.4的设备上,该条件始终为true。

At first, what's the specific error that you get? 首先,您会遇到什么具体错误?
It is possible that it isn't the problem of the device, but the Android version your are running. 可能不是设备问题,而是您正在运行的Android版本。

and try to change this: 并尝试更改此:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(policy);  

to: 至:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
 StrictMode.setThreadPolicy(policy);

Try to use AsyncTask to make your request. 尝试使用AsyncTask发出您的请求。

AsyncTask enables proper and easy use of the UI thread. 通过AsyncTask,可以正确,轻松地使用UI线程。 This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. 此类允许执行后台操作并在UI线程上发布结果,而无需操纵线程和/或处理程序。

The solution I've found so far is the following: 到目前为止,我发现的解决方案如下:

public static int lookupHost(String hostname) {
    // Hostname is to be `XXX.XXX.XXX.XXX` or `XXX.XXX.XXX.XXX:XXXX`
    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 (new Long( Long.parseLong(result, 16) )).intValue();
}

It looks to be working on most devices I've tested. 它似乎可以在我测试过的大多数设备上使用。

Problem solved. 问题解决了。 It can't find the route to IP while the wifi is enabled. 启用wifi时找不到IP的路由。 Simplest way is to disable wifi, do your stuff and then enable wifi. 最简单的方法是禁用wifi,先做一些事情,然后再启用wifi。

Here is the code I used: 这是我使用的代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM