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 Building an HTTP Request

What is missing or wrong?

In our movie app we want to build an HTTP request using OkHttp (TreeHttp had some bugs). Create a new OkHttpClient variable and initialize it using the default constructor.

MovieActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MovieActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie);

        // Get some movie information!
        String apiUrl = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=xyz&q=hobbit";




        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);
        }






    }
}

3 Answers

Stephen Bone
Stephen Bone
12,359 Points

Hi ma ali

It looks like you've just copied and pasted straight from the Stormy project which is fine but you've got some extra stuff in there that we don't need and you need to update the parameter so that it applies to this challenge.

So we only need the lines starting OkHttpClient... (which you write to complete the first task) and the line starting Request... (for second task). You can delete the Call and try/catch block.

We then just need to pass the url from this challenge, in this case apiUrl, into the Request.Builder.

So when you're done your code should look something like below:

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

Hope it helps!

Stephen

Can you paste the whole code please? I don't understand what you are trying to say.

OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("http://publicobject.com/helloword.text") .build();

import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button;

public class MovieActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_movie);

    // Get some movie information!
    String apiUrl = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=xyz&q=hobbit";

  OkHttpClient client = new OkHttpClient(); 
  Request request = new Request.Builder() .url("http://publicobject.com/helloword.text") .build();
}

}