Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Weather App (2015) Networking Making an HTTP GET Request with OkHttp

Robert E Mawere
Robert E Mawere
1,458 Points

i receive no weather data

double latitude=37.8267; double longitude=-122.4233;

    String forecast="https://api.darksky.net/forecast/" + apiKey +  "/" + latitude + "," + longitude  ;


    OkHttpClient client= new OkHttpClient();
    Request request=new Request.Builder().url(forecast).build();
    Call call=client.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {


            try {
                Log.v(TAG,response.body().string());
                if(response.isSuccessful()){

                    alertDialogNot();

                }

            } catch (IOException e) {
                Log.e(TAG,"exception cautch: ",e);
            }

        }
    });
}

private void alertDialogNot() {
    DialogFragment dialog=new DialogFragment();
    dialog.show(getFragmentManager(),"eror_dialog");
}

}

Robin Damsgaard Larsen
Robin Damsgaard Larsen
13,929 Points

What error are you getting? I see the onFailure() method doesn't do anything; maybe you could try to log if it's getting called, and possibly even the IOException as well.

3 Answers

Robert E Mawere
Robert E Mawere
1,458 Points

Thank you Robin...i am complete a begginer, what i have notice is this

call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) {

    }

    @Override
    public void onResponse(Call call, Response response) 

from teachers notice it is (Call call, IOException e) but mine is this (Request request, IOException e).i am not sure if it is a problem

Robin Damsgaard Larsen
Robin Damsgaard Larsen
13,929 Points

I understand your confusion about the onFailure(Request request, IOException e) method signature you mention. This is how it is shown in the video, but in your code in your initial post it is shown with a Call object instead of Request.

Looking at the current OkHTTP documentation at OkHTTP Recepis, the asynchronous recipe is indeed shown as onFailure(Call call, IOException e), which probably means that the OkHTTP verision in the video has been updated since then, and thus this method along with it. Nothing to worry about.

Looking at the asynchronous recipe again on the OkHTTP Recepis page, e.printStackTrace(); is called in the onFailure method, which will show some error text in LogCat if call.enqueue(Callback() {...}) fails miserably. Since your code isn't working, that is what I will suggest you do.

Robert E Mawere
Robert E Mawere
1,458 Points

thank you for quick reply let me keep trying