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

Nyle Cohen
Nyle Cohen
448 Points

Error even though i am following along

package com.teamrandom.stormy;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    public static final String TAG = MainActivity.class.getSimpleName();

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

        String apiKey = "9e64526f3fcfdfc5b4461b124ab998ea";
        double latitude = 37.8267 ;
        double longitude = 122.4233 ;
        String forecastUrl = "https://api.darksky.net/forecast/" + apiKey + "/" + latitude + "," + longitude;

        OkHttpClient client = new OkHttpClient();
        Request request = new request.Builder().url(forecastUrl).build();

        Call call = client.newCall(request);
        try {
            Response response = call.execute();
            if (response.isSuccessful()) {
                Log.v(TAG, response.body().string());
            }
        } catch (IOException e) {
            Log.e(TAG, "Exception caught : ", e);
        }
    }
}

The error is in here

OkHttpClient client = new OkHttpClient();
        Request request = new request.Builder().url(forecastUrl).build();

1 Answer

Fahad Mutair
Fahad Mutair
10,359 Points

hi Nyle Cohen , you have typo here new request.Builder()

Request request = new Request.Builder()
Nyle Cohen
Nyle Cohen
448 Points

Thanks. this works now! :)