How to use 3G Connection in Android Application instead of Wi-fi?
Asked Answered
E

9

31

How to use 3G Connection in Android Application instead of Wi-fi?

I want to connect a 3G connection, is there any sample code to connect to 3G instead of Wi-fi?

Esperanto answered 25/3, 2010 at 7:26 Comment(8)
Why not open the wireless connection preferences and let the user disable wifi himself. Pretty intrusive, but the user is in control.Seringa
I was looking for a way to do this but I was searching for the words "priority" (which I think would be the right name for it) instead of "preference". Thanks for the great question and answer.Chlorosis
@Seringa If you want to distribute load across network interfaces, or you trust sensitive information on one interface and want to route sensitive traffic via the trusted channel and other traffic over the untrusted channel, or you're worried one of the interfaces is more likely to drop out during a large download, or you just want to give the user sane, fine-grained application-specific permissions, you don't want to open the global settings page to address an application-specific concern.Thorstein
@Seringa What if one app always wants to use wifi, and another app always wants to use mobile data, both interfaces are up and the user wants to use both apps? There's no technical reason both those apps shouldn't be able to run, but if the developers of both those apps used your solution, the user would be seriously annoyed and, perhaps more importantly, nothing useful would happen.Thorstein
@ParthianShot you have valid points :). But the use cases you specify are pretty specific use cases I think, and more generally apps should probably use system wide settings.Seringa
@Seringa But the use cases you specify are pretty specific use cases True. Though they're also the kind of use cases which cause people to ask this type of question / find this question via google, hence the mention.Thorstein
@ParthianShot I agree :).Seringa
All those problems are because people don't know what IP is. They all are very bad programmers. Writing networking code is a complex task, but not impossible. I wonder why people could learn OpenGL but couldn't learn BSD sockets. android.stackexchange.com/a/106182/103522Messenia
C
13

The T-Mobile 'My Account" app does this, if you are connected to a WiFi connection it tells you that their program will not work over WiFi and then asks the user if they want to turn off the WiFi connection. If you choose "No" then the application exits, if you choose "Yes" then the app turns off your WiFi connection and then continues with starting up.

I think this is a good model to follow, it will ensure that your app is not being ran over WiFi and allows the user to decide if they want to turn off WiFi or not. An improvement on this model would be to turn wifi back on when the user navigates away from your app.

I haven't tested the following code, but it looks like it should work (modified from here)

use the following permissions in your manifest

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

and here is some actual code to turn wifi on/off

private WifiManager wifiManager;

@Override 
public void onCreate(Bundle icicle)
{
    ....................

    wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

    if(wifiManager.isWifiEnabled())
    {
        wifiManager.setWifiEnabled(false);
    }
    else
    {
        wifiManager.setWifiEnabled(true);
    }
}

If you do not want to go down that route it looks like you might be able to tell the phone that you would prefer to use the mobile data network rather than the wifi network.

The Android ConnectivityManager offers a function setNetworkPreference. This function is not really documented as you can tell if you click the link. I would paly around with it though because the constants that are defined seem to hint that you can set this to either TYPE_MOBILE or TYPE_WIFI and there is a DEFAULT_NETWORK_PREFERENCE constant as well that is defined as being 0x00000001 which is the same as TYPE_WIFI. So try getting access to a ConnectivityManager by calling

Context.getSystemService(Context.CONNECTIVITY_SERVICE);

and then try using the setNetworkPreference() function.

It doesn't appear to require any permissions in the manifest but it might require the CHANGE_NETWORK_STATE permission or something along those lines.

If you do sue the setNetworkPreference function it would probably be best to also set the Network Preference back to its original values (received from getNetworkPreference)

I hope this helps.

Choleric answered 25/3, 2010 at 15:46 Comment(1)
Although helpful, the answer below from @Burschenschaft is the correct answer.Tlaxcala
B
61
/**
 * Enable mobile connection for a specific address
 * @param context a Context (application or activity)
 * @param address the address to enable
 * @return true for success, else false
 */
private boolean forceMobileConnectionForAddress(Context context, String address) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (null == connectivityManager) {
        Log.debug(TAG_LOG, "ConnectivityManager is null, cannot try to force a mobile connection");
        return false;
    }

    //check if mobile connection is available and connected
    State state = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState();
    Log.debug(TAG_LOG, "TYPE_MOBILE_HIPRI network state: " + state);
    if (0 == state.compareTo(State.CONNECTED) || 0 == state.compareTo(State.CONNECTING)) {
        return true;
    }

    //activate mobile connection in addition to other connection already activated
    int resultInt = connectivityManager.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "enableHIPRI");
    Log.debug(TAG_LOG, "startUsingNetworkFeature for enableHIPRI result: " + resultInt);

    //-1 means errors
    // 0 means already enabled
    // 1 means enabled
    // other values can be returned, because this method is vendor specific
    if (-1 == resultInt) {
        Log.error(TAG_LOG, "Wrong result of startUsingNetworkFeature, maybe problems");
        return false;
    }
    if (0 == resultInt) {
        Log.debug(TAG_LOG, "No need to perform additional network settings");
        return true;
    }

    //find the host name to route
    String hostName = StringUtil.extractAddressFromUrl(address);
    Log.debug(TAG_LOG, "Source address: " + address);
    Log.debug(TAG_LOG, "Destination host address to route: " + hostName);
    if (TextUtils.isEmpty(hostName)) hostName = address;

    //create a route for the specified address
    int hostAddress = lookupHost(hostName);
    if (-1 == hostAddress) {
        Log.error(TAG_LOG, "Wrong host address transformation, result was -1");
        return false;
    }
    //wait some time needed to connection manager for waking up
    try {
        for (int counter=0; counter<30; counter++) {
            State checkState = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState();
            if (0 == checkState.compareTo(State.CONNECTED))
                break;
            Thread.sleep(1000);
        }
    } catch (InterruptedException e) {
        //nothing to do
    }
    boolean resultBool = connectivityManager.requestRouteToHost(ConnectivityManager.TYPE_MOBILE_HIPRI, hostAddress);
    Log.debug(TAG_LOG, "requestRouteToHost result: " + resultBool);
    if (!resultBool)
        Log.error(TAG_LOG, "Wrong requestRouteToHost result: expected true, but was false");

    return resultBool;
}

And this for calculate host address:

/**
 * This method extracts from address the hostname
 * @param url eg. http://some.where.com:8080/sync
 * @return some.where.com
 */
public static String extractAddressFromUrl(String url) {
    String urlToProcess = null;

    //find protocol
    int protocolEndIndex = url.indexOf("://");
    if(protocolEndIndex>0) {
        urlToProcess = url.substring(protocolEndIndex + 3);
    } else {
        urlToProcess = url;
    }

    // If we have port number in the address we strip everything
    // after the port number
    int pos = urlToProcess.indexOf(':');
    if (pos >= 0) {
        urlToProcess = urlToProcess.substring(0, pos);
    }

    // If we have resource location in the address then we strip
    // everything after the '/'
    pos = urlToProcess.indexOf('/');
    if (pos >= 0) {
        urlToProcess = urlToProcess.substring(0, pos);
    }

    // If we have ? in the address then we strip
    // everything after the '?'
    pos = urlToProcess.indexOf('?');
    if (pos >= 0) {
        urlToProcess = urlToProcess.substring(0, pos);
    }
    return urlToProcess;
}

/**
 * Transform host name in int value used by {@link ConnectivityManager.requestRouteToHost}
 * method
 *
 * @param hostname
 * @return -1 if the host doesn't exists, elsewhere its translation
 * to an integer
 */
private static int lookupHost(String hostname) {
    InetAddress inetAddress;
    try {
        inetAddress = InetAddress.getByName(hostname);
    } catch (UnknownHostException e) {
        return -1;
    }
    byte[] addrBytes;
    int addr;
    addrBytes = inetAddress.getAddress();
    addr = ((addrBytes[3] & 0xff) << 24)
            | ((addrBytes[2] & 0xff) << 16)
            | ((addrBytes[1] & 0xff) << 8 )
            |  (addrBytes[0] & 0xff);
    return addr;
}

And following permission must be added to AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

It works only with android 2.2 and above, tested on both Nexus One and on LG Optimus, other phones I don't know because some method of ConnectivityMananger are vendor-specific. After 15-20 seconds of inactivity, mobile network is automatically disconnected.

Burschenschaft answered 21/1, 2011 at 8:3 Comment(6)
I can't upvote this enough. This does exactly what I needed and worked like a charm without having to change anything. And it's the only good solution I found for that problem on the whole internet.Alainealair
I was a bit quick with the "worked like a charm". I have to call startUsingNetworkFeature regularly (every 30 seconds or so) or it will disconnect me even while I'm transmitting data. With this workaround I haven't run into any other problems yet though. Thank you!Alainealair
in addition to this excellent answer, if someone wants to turn-on a powered-off data connection i will suggest to add a call to the method from this answer: https://mcmap.net/q/353519/-enable-disable-data-connection-in-android-programmatically that can be called before just this line: "for (int counter=0; counter<30; counter++)" this way if the switch of data is off, it will work as wellCorvette
not working anymore with the api changes in Marshmallow. see developer.android.com/reference/android/net/…, java.lang.String)Vivavivace
Don't parse URLs yourself. See java.net.URL.Emission
You have mentioned "After 15-20 seconds of inactivity, mobile network is automatically disconnected" Is not really true...Racy
C
13

The T-Mobile 'My Account" app does this, if you are connected to a WiFi connection it tells you that their program will not work over WiFi and then asks the user if they want to turn off the WiFi connection. If you choose "No" then the application exits, if you choose "Yes" then the app turns off your WiFi connection and then continues with starting up.

I think this is a good model to follow, it will ensure that your app is not being ran over WiFi and allows the user to decide if they want to turn off WiFi or not. An improvement on this model would be to turn wifi back on when the user navigates away from your app.

I haven't tested the following code, but it looks like it should work (modified from here)

use the following permissions in your manifest

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

and here is some actual code to turn wifi on/off

private WifiManager wifiManager;

@Override 
public void onCreate(Bundle icicle)
{
    ....................

    wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

    if(wifiManager.isWifiEnabled())
    {
        wifiManager.setWifiEnabled(false);
    }
    else
    {
        wifiManager.setWifiEnabled(true);
    }
}

If you do not want to go down that route it looks like you might be able to tell the phone that you would prefer to use the mobile data network rather than the wifi network.

The Android ConnectivityManager offers a function setNetworkPreference. This function is not really documented as you can tell if you click the link. I would paly around with it though because the constants that are defined seem to hint that you can set this to either TYPE_MOBILE or TYPE_WIFI and there is a DEFAULT_NETWORK_PREFERENCE constant as well that is defined as being 0x00000001 which is the same as TYPE_WIFI. So try getting access to a ConnectivityManager by calling

Context.getSystemService(Context.CONNECTIVITY_SERVICE);

and then try using the setNetworkPreference() function.

It doesn't appear to require any permissions in the manifest but it might require the CHANGE_NETWORK_STATE permission or something along those lines.

If you do sue the setNetworkPreference function it would probably be best to also set the Network Preference back to its original values (received from getNetworkPreference)

I hope this helps.

Choleric answered 25/3, 2010 at 15:46 Comment(1)
Although helpful, the answer below from @Burschenschaft is the correct answer.Tlaxcala
S
5

I think that is not possible from Java. The system shuts down all mobile network based communication if connected to a wireless network. I think that you aren't allowed to start a 3G connection from you program.

Slap answered 25/3, 2010 at 9:49 Comment(3)
thank q if the mobile user enable both means 3G and WiFi that time my application running failed because of it is continuously jumping WiFi to 3G and 3G to WiFi can u pl give any alternative solutionCohe
There is no way around this. If the user has a very spoty 3G connection she will get network and loose network connection all the time too. The only thing you can do is do small and frequent requests to your server and don't try to keep a connection open for a longer time. And then handle the network errors in an appropriate way maybe retrying it or advising the user to try it later with a more stable connection.Slap
I am very sorry, but things on Android are really so bad. I have tested 2 BSD sockets binded ane to Wi-Fi and another to 3G IP addresses. Android really blocks one socket (not receives packets) when it switches to another network type. If you use connectivityManager.requestRouteToHost(ConnectivityManager.TYPE_MOBILE_HIPRI, hostAddress); with connectivityManager.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "enableHIPRI"); you have a socket which you bind to another address receiving a packet not corresponding to it - more bad.Messenia
M
4

Here is the code that works on API 21+ (Lollipop, Marshmallow..). I prefer to use OkHttp with Network.getSocketFactory(), but Network.openURLConnection() also works fine.

private void doTest()
{
    display("Requesting CELLULAR network connectivity...");
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

    NetworkRequest request = new NetworkRequest.Builder()
            .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
            .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build();

    connectivityManager.requestNetwork(request, new ConnectivityManager.NetworkCallback()
    {
        /**
         * Called when the framework connects and has declared a new network ready for use.
         * This callback may be called more than once if the {@link Network} that is
         * satisfying the request changes.
         *
         * This method will be called on non-UI thread, so beware not to use any UI updates directly.
         *
         * @param network The {@link Network} of the satisfying network.
         */
        @Override
        public void onAvailable(final Network network)
        {
            display("Got available network: " + network.toString());

            try
            {
                final InetAddress address = network.getByName("navalclash.com");
                display("Resolved host2ip: " + address.getHostName() + " -> " +  address.getHostAddress());
            }
            catch (UnknownHostException e)
            {
                e.printStackTrace();
            }

            display("Do request test page from remote http server...");

            if(okHttpClient == null)
            {
                okHttpClient = new OkHttpClient.Builder().socketFactory(network.getSocketFactory()).build();
            }

            Request request = new Request.Builder()
                    .url("http://navalclash.com")
                    .build();
            try (Response response = okHttpClient.newCall(request).execute())
            {
                display("RESULT:\n" + response.body().string());
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
    });
}
Merrymaking answered 16/6, 2016 at 14:56 Comment(1)
OkHttp didn't seem to work for me (which is strange), but network.openURLConnection() did.Amsterdam
P
2

Use connection manager and set network preference as you want.

for example:

dataManager  = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
dataManager.setNetworkPreference(ConnectivityManager.TYPE_MOBILE);
Presignify answered 5/1, 2013 at 19:50 Comment(1)
ConnectivityManager.setNetworkPreference() is close to be obsoleted. But what is more important, if you do getNetworkPreference() before changing, it will return ConnectivityManager.TYPE_MOBILE. Setting it there does not make any difference. As for HIPRI itself, it works in pretty strange way. First, it allows connections to all hosts, not only to those explicitly requested by routing. Second, when you turn it off with stopUsingFeature...() call, it will not be turned off and will be still active. Third, all applications will use it even if wifi is available, which contradicts documentation.Broadway
B
2

Inspired by code in this ticket and using some parts of it, here is service that establishes hipri mobile and keeps it running.

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.atomic.AtomicBoolean;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.os.IBinder;
import android.util.Log;

public class HipriService extends Service {
    private AtomicBoolean enabledMobile = new AtomicBoolean(false);

    public boolean enableMobileConnection() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        if (null == cm) {
            Log.d(TAG, "ConnectivityManager is null, cannot try to force a mobile connection");
            return false;
        }

        /*
         * Don't do anything if we are connecting. On the other hands re-new
         * connection if we are connected.
         */
        State state = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState();
        Log.d(TAG, "TYPE_MOBILE_HIPRI network state: " + state);
        if (0 == state.compareTo(State.CONNECTING))
            return true;

        /*
         * Re-activate mobile connection in addition to other connection already
         * activated
         */
        int resultInt = cm.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "enableHIPRI");
        //Log.d(TAG, "startUsingNetworkFeature for enableHIPRI result: " + resultInt);

        //-1 means errors
        // 0 means already enabled
        // 1 means enabled
        // other values can be returned, because this method is vendor specific
        if (-1 == resultInt) {
            Log.e(TAG, "Wrong result of startUsingNetworkFeature, maybe problems");
            return false;
        }
        if (0 == resultInt) {
            Log.d(TAG, "No need to perform additional network settings");
            return true;
        }

        return requestRouteToHost(this, Uploader.ServerAddress);
    }

    private Thread pingerThread = null;

    private void startMobileConnection() {
        enabledMobile.set(true);
        pingerThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (enabledMobile.get()) {
                    /*
                     * Renew mobile connection. No routing setup is needed. This
                     * should be moved to 3g monitoring service one day.
                     */
                    enableMobileConnection();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // do nothing
                    }
                }
            }
        });
        pingerThread.start();
    }

    private void stopMobileConnection() {
        enabledMobile.set(false);
        disableMobileConnection();
        pingerThread.interrupt();
        pingerThread = null;
    }

    public void disableMobileConnection() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        cm.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "enableHIPRI");
    }

    public final static int inetAddressToInt(InetAddress inetAddress) {
        byte[] addrBytes;
        int addr;
        addrBytes = inetAddress.getAddress();
        addr = ((addrBytes[3] & 0xff) << 24) | ((addrBytes[2] & 0xff) << 16) | ((addrBytes[1] & 0xff) << 8)
                | (addrBytes[0] & 0xff);
        return addr;
    }

    public final static InetAddress lookupHost(String hostname) {
        try {
            return InetAddress.getByName(hostname);
        } catch (UnknownHostException e) {
            return null;
        }
    }

    private boolean requestRouteToHost(Context context, String hostname) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (null == cm) {
            Log.d(TAG, "ConnectivityManager is null, cannot try to force a mobile connection");
            return false;
        }

        /* Wait some time needed to connection manager for waking up */
        try {
            for (int counter = 0; enabledMobile.get() && counter < 30; counter++) {
                State checkState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState();
                Log.i(TAG, "Waiting for mobile data on. State " + checkState);
                if (0 == checkState.compareTo(State.CONNECTED))
                    break;
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            //nothing to do
        }

        if (!enabledMobile.get()) {
            Log.d(TAG, "Mobile data is turned off while waiting for routing.");
            return false;
        }

        State checkState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState();
        if (0 != checkState.compareTo(State.CONNECTED)) {
            Log.e(TAG, "Mobile data is still turned off after 30 sec of waiting.");
            return false;
        }
        Log.i(TAG, "Adding routing for " + hostname);

        InetAddress inetAddress = lookupHost(hostname);
        if (inetAddress == null) {
            Log.e(TAG, "Failed to resolve " + hostname);
            return false;
        }
        int hostAddress = inetAddressToInt(inetAddress);

        boolean resultBool = cm.requestRouteToHost(ConnectivityManager.TYPE_MOBILE_HIPRI, hostAddress);
        Log.d(TAG, "requestRouteToHost result: " + resultBool);
        if (!resultBool)
            Log.e(TAG, "Wrong requestRouteToHost result: expected true, but was false");

        return resultBool;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        startMobileConnection();
    }

    @Override
    public void onDestroy() {
        stopMobileConnection();
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

Here is how I start/stop it when needed. Note that it also gets locks on cpu and wifi so that it may run when the phone sleeps (screen only). Wifi is needed because my app is kind of bridge between wifi and mobile connections. You may not need it.

public void startMobileData() {
    if (!enabledMobile.get()) {
        enabledMobile.set(true);
        WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL, "Wifi Wakelock");
        wifiLock.acquire();

        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        partialLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "3G Wakelock");
        partialLock.acquire();

        startService(new Intent(this, HipriService.class));
    }
}

public void stopMobileData() {
    if (enabledMobile.get()) {
        enabledMobile.set(false);
        Log.i(TAG, "Disabled mobile data");
        stopService(new Intent(this, HipriService.class));

        if (partialLock != null) {
            partialLock.release();
            partialLock = null;
        }

        if (wifiLock != null) {
            wifiLock.release();
            wifiLock = null;
        }
    }
}

Don't forget to add service to you manifest file.

Broadway answered 22/12, 2013 at 9:33 Comment(0)
S
1

This aplication activate 3G and Wifi connection, Giving preference to 3G!! Very useful http://www.redrails.com.br/2012/02/wireless-analyzer-for-android/

Sternforemost answered 3/2, 2012 at 22:0 Comment(0)
G
0

@umka

  • i think, app can only reach to those hosts through HIPRI which it has requested, may be for other hosts(whose routes are not requested) is using the default network(MOBILE or WIFI).
  • On calling stopUsingNetworkFeature(), It will check -- is there any other app is using this network, if yes, then it will ignore your request to down this network feature.
  • one of the main purpose of HIPRI network is that - if wifi is on and an app wants to use mobile netwrok(3G)to reach the particular host, it can reach through HIPRI network.
Gunning answered 25/1, 2014 at 15:25 Comment(0)
G
0

There is a missing piece of code on @Northern Captain answer, the DNS lookup code.

Here is a working code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

    Log.d("NetworkDns", "Requesting CELLULAR network connectivity...");

    NetworkRequest request = new NetworkRequest.Builder()
            .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
            .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build();

    connectivityManager.requestNetwork(request, new ConnectivityManager.NetworkCallback()
    {
        @Override
        public void onAvailable(final Network network)
        {
            Log.d("NetworkDns", "Got available network: " + network.toString());

            try
            {
                final InetAddress address = network.getByName("www.website.com");
                Log.d("NetworkDns", "Resolved host2ip: " + address.getHostName() + " -> " +  address.getHostAddress());
            }
            catch (UnknownHostException e)
            {
                e.printStackTrace();
            }

            Log.d("NetworkDns", "Do request test page from remote http server...");

            OkHttpClient okHttpClient = null;

            if(okHttpClient == null)
            {
                okHttpClient = new OkHttpClient.Builder()
                        .socketFactory(network.getSocketFactory())
                        .dns(new Dns() {
                            @Override
                            public List<InetAddress> lookup(String hostname) throws UnknownHostException {
                                if (network != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                                    List<InetAddress> addresses = Arrays.asList(network.getAllByName(hostname));
                                    Log.d("NetworkDns", "List : " + addresses);
                                    return addresses;
                                }
                                return SYSTEM.lookup(hostname);
                            }
                        }).build();
            }

            Request request = new Request.Builder()
                    .url("http://www.website.com")
                    .build();
            try (Response response = okHttpClient.newCall(request).execute())
            {
                Log.d("NetworkDns", "RESULT:\n" + response.body().string());
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }

        }
    });
}
Gazzo answered 30/5, 2018 at 21:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.