Search code examples
javaandroidftpsonysony-camera-api

IOException Unable to resolve host,No address associated with hostname


I am working on Sony Remote camera which is connected using WiFi.I need to click a picture using a camera and then upload it to my FTP server which is in another activity.for than I need to disconnect my camera wifi and connect to the another wifi network or mobile data.when I connect to the another wifi/mobile data and going to upload the picture on FTP server I got this error.

IOException Unable to resolve host No address associated with hostname

When a close application and start again, And then directly upload pictures without connecting/disconnection camera than it works fine. someone, please tell me how can I solve this, because I checked each and every solution on stack overflow and not one solution work for me.

I added bellow permissions

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

Solution

  • Connection to a network is not inmediate. Also, if the desired network has no internet connection, in recent versions of Android it will not connect (if the connection is made by a user, a popup shows a confirmation to connnect).

    I solved both problems using Android APIs (2 versions, one for API<21 and the other for API>=21).

    Try this code (I'm using AndroidAnnotations for dependency injection, but is not required):

    public class WifiHelper extends BroadcastReceiver {
        @RootContext
        Context context;
    
        @SystemService
        ConnectivityManager connectivityManager;
    
        @SystemService
        WifiManager wifiManager;
    
        // For API>=21
        private WifiHelperNetworkCallback wnc;
    
        // For API<21
        private boolean isBroadcastRegistered;
    
        private String desiredSSID;
    
        private Runnable callback;
    
        public void enableNetwork(String ssid, int networkId, Runnable callback) {
            desiredSSID = ssid;
            wifiManager.enableNetwork(networkId, true);
            configureNetworkRequest();
        }
    
        private void networkAvailable() {
            // this method will be called when the network is available
            callback.run();
        }
    
        private void configureNetworkRequest() {
            if (android.os.Build.VERSION.SDK_INT >= 21) {
                configureNetworkRequestAPI21();
            } else {
                configureNetworkRequestLegacy();
            }
        }
    
        @TargetApi(21)
        private void configureNetworkRequestAPI21() {
            NetworkRequest request = new NetworkRequest.Builder()
                    .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                    .build();
            if (wnc == null) wnc = new WifiHelperNetworkCallback(this, connectivityManager);
    
            connectivityManager.requestNetwork(request, wnc);
        }
    
        private void configureNetworkRequestLegacy() {
            unregisterReceiver();
    
            connectivityManager.startUsingNetworkFeature(ConnectivityManager.TYPE_WIFI, null);
            IntentFilter intent = new IntentFilter();
            intent.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
            intent.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    
            context.registerReceiver(this, intent);
            isBroadcastRegistered = true;
        }
    
        @TargetApi(21)
        private void disableNetworkRequest() {
            if (android.os.Build.VERSION.SDK_INT >= 21) {
                if (wnc != null) connectivityManager.unregisterNetworkCallback(wnc);
    
                ConnectivityManager.setProcessDefaultNetwork(null);
            } else {
                unregisterReceiver();
            }
        }
    
        private void unregisterReceiver() {
            if (isBroadcastRegistered) {
                context.unregisterReceiver(this);
                isBroadcastRegistered = false;
            }
        }
    
        // API<21
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
                NetworkInfo networkInfo =
                        intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
                if(networkInfo.isConnected()) {
                    // Wifi is connected
    
                    if (desiredSSID.equals(getCurrentSSID())) {
                        // Callback and unregister
                        networkAvailable();
                        unregisterReceiver();
                    }
                }
            }
        }
    
        public String getCurrentSSID() {
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            if (wifiInfo != null && wifiInfo.getSupplicantState()== SupplicantState.COMPLETED) {
                return ssidWithoutQuotes(wifiInfo.getSSID());
            }
            else return null;
        }
    
        protected static String ssidWithoutQuotes(String ssid) {
            if (ssid == null) return null;
            else if (ssid.startsWith("\"") && ssid.endsWith("\"")) {
                return ssid.substring(1, ssid.length() - 1);
            } else {
                return ssid;
            }
        }
    
        protected String getDesiredSSID() {
            return desiredSSID;
        }
    
        @TargetApi(21)
        public static class WifiHelperNetworkCallback extends ConnectivityManager.NetworkCallback {
            public final String LOG_TAG = WifiHelper.class.getSimpleName();
    
            private ConnectivityManager connectivityManager;
            private WifiHelper wifiHelper;
    
            public WifiHelperNetworkCallback(WifiHelper wifiHelper, ConnectivityManager connectivityManager) {
                this.wifiHelper = wifiHelper;
                this.connectivityManager = connectivityManager;
            }
    
            public void onAvailable(Network network) {
                // Do something once the network is available
                NetworkInfo info = connectivityManager.getNetworkInfo(network);;
    
                Log.i(LOG_TAG, "networkcallback!! " + info.getExtraInfo());
    
                String desiredSSID = wifiHelper.getDesiredSSID();
    
                if (desiredSSID != null && desiredSSID.equals(ssidWithoutQuotes(info.getExtraInfo()))) {
                    ConnectivityManager.setProcessDefaultNetwork(network);
                    wifiHelper.networkAvailable();
                }
            }
    
        }
    
    }
    

    You will need this permissions:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.VIBRATE" android:maxSdkVersion="18"/>