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

Guy Bridge
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Guy Bridge
Android Development Techdegree Graduate 24,991 Points

Retrofit and Youtube

Hi Guys,

I'm trying to get some json data from YouTube using Retrofit. I would usually just use OKHttp in a custom API call class and then parse the json with the JsonObject class.

This works great but a requirement of the assignment is to use retrofit. Perhaps some one can explain why it's better to use retrofit.

So the issue is that the app crashes on parsing the json output to string because the response is null;

Interface;

public interface YouTubeApiService
{
    String YOUTUBE_SEARCH_BASE_URL = "https://www.googleapis.com/youtube/v3/";
    // Example

    @GET("search?part=snippet&maxResults=50")
    Call<ResponseBody> results(@Query("q") String q);

}

The search;

 private void search()
    {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(YouTubeApiService.YOUTUBE_SEARCH_BASE_URL)
                .build();

        YouTubeApiService service = retrofit.create(YouTubeApiService.class);

        Call<ResponseBody> searchCall = service.results("android test search");
        searchCall.enqueue(new Callback<ResponseBody>()
        {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
            {
                try
                {
                    Log.i(TAG, "Response is: " + response.body().string());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t)
            {

            }
        });

    }

I also don't know where to put the API key in?

Thanks for you help.

Guy Bridge
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Guy Bridge
Android Development Techdegree Graduate 24,991 Points

In the end, I was missing the API key; so you would need to add this;

@GET("search?key=" + Constants.YOUTUBE_API_KEY + "&part=snippet&type=video&maxResults=50") Call<ResponseBody> results(@Query("q") String query);

You can also do this to view the URL that Retrofit makes so you can view it in a browser, if it's null or you get a json error back saying "missing key" then you know why the response is null.

Call<ResponseBody> searchCall = service.results(searchTerm);

Log.i(TAG, searchCall.request().toString());