如果 3G/4G 打开,Android volley 在本地 WiFi 上不起作用 [英] Android volley doesn't work on local WiFi if 3G/4G is ON

查看:44
本文介绍了如果 3G/4G 打开,Android volley 在本地 WiFi 上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序可以与 Ricoh Theta 相机进行通信.相机创建其 Wi​​Fi 网络和 OSC(开放球形相机)网络服务器(IP 192.168.1.1,端口 80),我在其上连接我的设备.如果只有 WiFi 开启,一切正常.但是当我也打开移动数据时,我收到超时错误.

I have an app that communicates with a Ricoh Theta camera. The camera creates its WiFi network and OSC (Open Spherical Camera) web server (IP 192.168.1.1, port 80), on which I connect my device. Everything works fine if only the WiFi is ON. But when I also put the mobile data ON, then I get a timeout error.

不确定它是否有用,但这里有一些代码:

No sure if it can be useful, but here is some code:

protected void executePost(String request, final String body, final RequestListener listener) {

    StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://" + mIpAddress + ":" + mPort + request,

            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    if(listener != null) {
                        handleResponse(response, listener);
                    }
                }
            },

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    if(listener != null) {
                        listener.onError(error);
                    }
                }
            }
    )
    {
        @Override
        public byte[] getBody() throws AuthFailureError {

            return body == null ? null : body.getBytes();
        }
    };

    int socketTimeout = 30000;
    RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    stringRequest.setRetryPolicy(policy);

    stringRequest.setTag(REQUEST_TAG);
    mRequestQueue.add(stringRequest);
}

有没有办法告诉 Volley 只使用 WiFi?还是先?

Is there a way to tell Volley to use the WiFi only? Or first?

推荐答案

好的,抱歉,经过一番研究我找到了解决方案,在这里:https://code.google.com/p/android/issues/detail?id=190974

OK, sorry, after some research I found the solution, here: https://code.google.com/p/android/issues/detail?id=190974

问题在于,从 Android 6.0 开始,如果设备连接到多个网络,Android 将连接到具有 Internet 访问权限的一个网络,而忽略其他网络.看起来很奇怪,有礼貌,但仍然......

The problem is that as of Android 6.0, if the device is connected to several networks, Android will connect to the one with an internet access, and ignore the other(s). Seems pretty weird, to be polite, but still...

这是我添加的代码以使其正常工作:

Here is the code I added to make it working:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {

    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    for (Network net : connectivityManager.getAllNetworks()) {

        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(net);

        if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            connectivityManager.bindProcessToNetwork(net);
            break;
        }
    }
}

这篇关于如果 3G/4G 打开,Android volley 在本地 WiFi 上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆