The implementation of Okhttp multi network communication

Posted by jordan00 on Wed, 01 Jan 2020 10:22:38 +0100

In recent project requirements, android needs to be able to access the internal and external networks at the same time, that is, different communications go through different networks, and it is feasible to check relevant information, but certain conditions are required

1. Prerequisites:

1) android system version on the control board needs to be more than 5.0
2) The android system on the control board needs to support the coexistence of multiple network connections at the same time, that is, to meet the requirements of WiFi network, mobile network and network card that can connect and assign IP addresses to their respective networks at the same time, and can not automatically disconnect the other network after connecting one network (example: the current control board is connected to WiFi, but automatically disconnects and closes WiFi connection after the network interface is connected to the network, and then Unable to connect WiFi manually).

2. When using the API provided by the system for network connection, select the corresponding network (example code is as follows)

final ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        //android system required5.0Above
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && connectivityManager != null) {
            NetworkRequest.Builder builder = new NetworkRequest.Builder();
            //Select the network to connect data. Here is WiFi. You can select different networks according to different connection needs
            builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
            NetworkRequest networkRequest = builder.build();
            connectivityManager.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback() {
                @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                @Override
                public void onAvailable(final Network network) {
                    super.onAvailable(network);
                    //After the network is connected
                    SocketFactory socketFactory = network.getSocketFactory();
                    //If you use the eclipse MQTT package, you need to set the
                    mqttConnectOptions.setSocketFactory(socketFactory);
                    //If you use regular network connection, okhttp, you need to set
                    OkHttpClient okHttpClient = new OkHttpClient.Builder()
                            //.proxy(Proxy.NO_PROXY)
                            .socketFactory(socketFactory)
                            .dns(new Dns() {
                                @Override
                                public List<InetAddress> lookup(@NonNull String hostname) throws UnknownHostException {
                                    return Arrays.asList(network.getAllByName(hostname));
                                }
                            })
                            .build();
                    Retrofit retrofit = new Retrofit.Builder()
                            .client(okHttpClient)
                            .baseUrl("http://wanandroid.com/")
                            .addConverterFactory(ScalarsConverterFactory.create())
                            .build();
                    apiService = retrofit.create(ApiService.class);
                    showMsg("Network connected  ");
                }
            });
        }

Topics: network Android Retrofit DNS