'java.net.UnknownHostException: Unable to resolve host "api.openweathermap.org": No address associated with hostname

Please help debug the following code I have made sure that

1.My Device has Internet access.

2.My Android Manifest file has internet permission.

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    private StringBuilder stringBuilder = null;
    private RequestQueue requestQueue;
    private String url = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=48b076c80a0b064d62117d19015db014";
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        textView = (TextView) findViewById(R.id.temp);

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                        try {
                            JSONObject jsonObject = response.getJSONObject("main");
                            float tempr = (float) jsonObject.getDouble("temp");
                            stringBuilder.append(tempr);

                            textView.setText(stringBuilder.toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), error.getMessage().toString(), Toast.LENGTH_LONG).show();
                Log.v("Not working", error.getMessage().toString());

            }
        });
        requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(jsonObjectRequest);
    }
}

The logcat shows the following report:

java.net.UnknownHostException: Unable to resolve host "api.openweathermap.org": No address associated with hostname

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="androidhive.com.volley5">

<uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
       <activity
           android:name=".MainActivity"
           android:label="@string/app_name"
           android:theme="@style/AppTheme.NoActionBar">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />

               <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
     </application>

   </manifest>


Solution 1:[1]

Faced the same issue when I was working with openweathermap API.

There are a few steps that solved my issue.

First of all, turn on emulators wifi and mobile data both.

Second, reset permission in androidmenifest.xml file. Add internet permission and also add read and write external storage.

Then run the app. Hope it will solve your error as it solved mine.

Solution 2:[2]

Hello. Today I faced this problem. There was simply no internet on the phone. I was helped by checking the connection to Wi-Fi

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Md Nakibul Hassan
Solution 2 ?????? PARSHIVEC