java.net.UnknownHostException: Unable to resolve host "<url>": No address associated with hostname and End of input at character 0 of

199,387

Solution 1

I encountered this problem too, reconnecting the WiFi can solve this.

For us ,we can check if the phone can resolve the host to IP when we start application. If it cannot resolve, tell the user to check the WiFi and then exit.

I hope it helps.

Solution 2

If reconnecting the WiFi doesn't work for you, try reboot your device.

This works for me. Hope it helps.

Solution 3

I had the same exception in the simulator (Android Studio on OSX) but connecting to the same URL on the iOS simulator worked fine... Looks like it all stemmed from the fact I'd be running the simulator whilst connected to a personal hotspot for my internet connection and then came back later while connected to wifi and the simulator didn't like the new internet connection for some reason, seems like it thought the old hotspot was the current connection, which was no longer working..

Closing and relaunching the simulator worked!

Solution 4

I got the same error and the issue was that I was on VPN and I didn't realize it. After disconnecting from the VPN and reconnecting to my WIFI network, the problem was resolved.

Solution 5

Missed to configure tag in manifest file

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Share:
199,387

Related videos on Youtube

Reshma
Author by

Reshma

Updated on July 08, 2022

Comments

  • Reshma
    Reshma almost 2 years

    I've created an app that loads a question from my web services, and it works fine. But, sometimes it crashes and I do not get the reason why this is happening, especially because I have also given it the required permissions. It works fine, but at random, it crashes and gives me this report.

    private void sendContinentQuestions(int id) {
        // TODO Auto-generated method stub
    
        //Get the data (see above)
        JSONArray json = getJSONfromURL(id);
            try{
                for(int i=0; i < json.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    JSONObject jObject = json.getJSONObject(i);
                    longitude":"72.5660200"
                    String category_id = jObject.getString("category_id");
                    String question_id = jObject.getString("question_id");
                    String question_name = jObject.getString("question_name");
                    String latitude = jObject.getString("latitude");
                    String longitude = jObject.getString("longitude");
                    String answer = jObject.getString("answer");
                    String ansLatLng = latitude+"|"+longitude ; 
                    Log.v("## data:: ###",question_id+"--"+question_name+"-cat id-"+category_id+"--ansLatLng "+ansLatLng+" answer: "+answer);
    
                    all_question.add(new QuestionData(game_id,category_id,question_id,question_name,ansLatLng,answer));
                }
            }catch(JSONException e)        {
                Log.e("log_tag", "Error parsing data "+e.toString());
            }
        }
    
    
    }
     public JSONArray getJSONfromURL(int id){
    
        String response = "";
        URL url;
        try {
            url = new URL(Consts.GET_URL+"index.php/Api/getQuestion?cat_id="+id);
            HttpURLConnection http = (HttpURLConnection) url.openConnection();
            http.setRequestMethod("POST");
            InputStream is = http.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            response = br.readLine();
            Log.v("###Response :: ###",response);
            http.disconnect();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (ProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace(); 
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        //try parse the string to a JSON object
        JSONArray jArray = null;
        try{
    
            jArray = new JSONArray(response);
    
        }catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
        }
    
        return jArray;
    }
    

    11-13 15:02:52.307: W/System.err(8012): java.net.UnknownHostException: Unable to resolve host "www.xyz.com": No address associated with hostname
    11-13 15:02:52.317: W/System.err(8012):     at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
    11-13 15:02:52.317: W/System.err(8012):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    11-13 15:02:52.317: W/System.err(8012):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
    11-13 15:02:52.317: W/System.err(8012):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
    11-13 15:02:52.317: W/System.err(8012):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
    11-13 15:02:52.317: W/System.err(8012):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
    11-13 15:02:52.317: W/System.err(8012):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
    11-13 15:02:52.317: W/System.err(8012):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
    11-13 15:02:52.317: W/System.err(8012):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
    11-13 15:02:52.317: W/System.err(8012):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
    11-13 15:02:52.317: W/System.err(8012):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
    11-13 15:02:52.317: W/System.err(8012):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
    11-13 15:02:52.317: W/System.err(8012):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
    11-13 15:02:52.317: W/System.err(8012):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
    11-13 15:02:52.327: W/System.err(8012):     at com.abc.xyz.ContinentActivity.getJSONfromURL(ContinentActivity.java:400)
    11-13 15:02:52.327: W/System.err(8012):     at com.abc.xyz.ContinentActivity.sendContinentQuestions(ContinentActivity.java:327)
    11-13 15:02:52.327: W/System.err(8012):     at com.abc.xyz.ContinentActivity.access$2(ContinentActivity.java:323)
    11-13 15:02:52.327: W/System.err(8012):     at com.abc.xyz.ContinentActivity$LoadQuestions.doInBackground(ContinentActivity.java:254)
    11-13 15:02:52.327: W/System.err(8012):     at com.abc.xyz.ContinentActivity$LoadQuestions.doInBackground(ContinentActivity.java:1)
    11-13 15:02:52.327: W/System.err(8012):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
    11-13 15:02:52.327: W/System.err(8012):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    11-13 15:02:52.327: W/System.err(8012):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    11-13 15:02:52.327: W/System.err(8012):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    11-13 15:02:52.337: W/System.err(8012):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    11-13 15:02:52.337: W/System.err(8012):     at java.lang.Thread.run(Thread.java:841)
    11-13 15:02:52.337: W/System.err(8012): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
    11-13 15:02:52.337: W/System.err(8012):     at libcore.io.Posix.getaddrinfo(Native Method)
    11-13 15:02:52.337: W/System.err(8012):     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
    11-13 15:02:52.337: W/System.err(8012):     at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
    11-13 15:02:52.337: W/System.err(8012):     ... 24 more
    11-13 15:02:52.337: E/log_tag(8012): Error parsing data org.json.JSONException: End of input at character 0 of 
    11-13 15:02:52.337: W/dalvikvm(8012): threadid=194: thread exiting with uncaught exception (group=0x417c1700)
    11-13 15:02:52.337: E/AndroidRuntime(8012): FATAL EXCEPTION: AsyncTask #5
    11-13 15:02:52.337: E/AndroidRuntime(8012): java.lang.RuntimeException: An error occured while executing doInBackground()
    11-13 15:02:52.337: E/AndroidRuntime(8012):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
    11-13 15:02:52.337: E/AndroidRuntime(8012):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
    11-13 15:02:52.337: E/AndroidRuntime(8012):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
    11-13 15:02:52.337: E/AndroidRuntime(8012):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
    11-13 15:02:52.337: E/AndroidRuntime(8012):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    11-13 15:02:52.337: E/AndroidRuntime(8012):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    11-13 15:02:52.337: E/AndroidRuntime(8012):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    11-13 15:02:52.337: E/AndroidRuntime(8012):     at java.lang.Thread.run(Thread.java:841)
    11-13 15:02:52.337: E/AndroidRuntime(8012): Caused by: java.lang.NullPointerException
    11-13 15:02:52.337: E/AndroidRuntime(8012):     at com.abc.xyz.ContinentActivity.sendContinentQuestions(ContinentActivity.java:328)
    11-13 15:02:52.337: E/AndroidRuntime(8012):     at com.abc.xyz.ContinentActivity.access$2(ContinentActivity.java:323)
    11-13 15:02:52.337: E/AndroidRuntime(8012):     at com.abc.xyz.ContinentActivity$LoadQuestions.doInBackground(ContinentActivity.java:254)
    11-13 15:02:52.337: E/AndroidRuntime(8012):     at com.abc.xyz.ContinentActivity$LoadQuestions.doInBackground(ContinentActivity.java:1)
    11-13 15:02:52.337: E/AndroidRuntime(8012):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
    11-13 15:02:52.337: E/AndroidRuntime(8012):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    
    • Apoorv
      Apoorv over 10 years
      Are you connected to the Internet?
    • GrIsHu
      GrIsHu over 10 years
      Do you get the response printed in your log ?
    • Reshma
      Reshma over 10 years
      Yes, I have checked for internet connection.
    • Admin
      Admin over 10 years
      Check if the phone can access this url (in the browser for exemple)
    • JPerk
      JPerk almost 6 years
      Do not try with simulator, I have had the same issue. And I have spent 1 day trying to figure it out. Finally, my solution was to use a real android device for testing.
    • Arpit J.
      Arpit J. over 2 years
      all these answers are bad seriously!!. For poor connectivity use-cases, UnknownHostException is thrown no matter what while being on the mobile network. Response just doesn't come and you should just timeout the request for handling such cases.
  • DemonGyro
    DemonGyro about 10 years
    THANK YOU SOOOO MUCH! I couldn't figure out why I could connect to the address but not access it through my app. Stupid WiFi bug...
  • Francisco Corrales Morales
    Francisco Corrales Morales almost 10 years
    I had to turn off my phone !
  • thomaspsk
    thomaspsk over 7 years
    Spent a long while trying to figure out why my app no longer worked... Of all the things it could have been, this was it. Thank you!!
  • X-HuMan
    X-HuMan almost 7 years
    Is this issue still exist in Android 7? I can encounter this behavior randomly on application open. The network connection is available for the application, but it connect access to the web services. What could cause to this? Several devices in the network have the same problem.
  • DragonFire
    DragonFire about 4 years
    Can there be some way to give alert or toast to user because programmers may understand the problem but the user does not he won't come to stackoverflow he will just uninstall the app..
  • Primož Ivančič
    Primož Ivančič almost 4 years
    I did the same thing, only with mobile data.
  • Tarun Deep Attri
    Tarun Deep Attri over 3 years
    man what a saviour. Seems like some bug of wifi. Disconnecting worked like a charm.
  • Abhijit Chakra
    Abhijit Chakra over 3 years
    This solution worked for me with Disconnecting VPN
  • user2342558
    user2342558 over 3 years
    Your solution worked for me! Thanks a lot! Experienced on Samsung Galaxy M31 with Android 10.
  • Phantom Lord
    Phantom Lord almost 3 years
    This is not the solution, it is very restrictive
  • gumuruh
    gumuruh about 2 years
    omg... a wifi problem not the android problems....! i should put the code for checking whether the internet is exist or not first. Thanks @Jiyeh