2

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>
4
  • What device are you testing on? Have you retried again? Commented Jun 2, 2016 at 16:43
  • 1
    I tried on Genymotion emulator Samsung Galaxy S5 4.4.4-API 19-1080X1920 Commented Jun 2, 2016 at 16:45
  • How does your manifest look like? Commented Jun 2, 2016 at 16:46
  • 2
    @Felix Edelmann i have edited my question and added the manifest file.. Commented Jun 2, 2016 at 16:54

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.