I'm trying to make a call to youtube data API to retrieve the subscriber count of a particular channel. But I don't know how to implement the API interface and define the endpoints so I pasted my whole remaining URL in the @GET method. But my app is crashing when started.
My complete URL is: https://www.googleapis.com/youtube/v3/channels?part=statistics&id=+UC-lHJZR3Gqxm24_Vd_AJ5Yw&key=AIzaSyAyON6YdgkFrtNHrGGs3IFS4groadJhhts
Here is my interface :
public interface ApiInterface
{
@GET("/channels?part=statistics&id=+UC-lHJZR3Gqxm24_Vd_AJ5Yw&key=AIzaSyAyON6YdgkFrtNHrGGs3IFS4groadJhhts")
Call<Mainjson> getMainJson();
}
Main Activity :
public class MainActivity extends AppCompatActivity
{
private Statistics statistics;
private String subscribers;
private TextView subscribersPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
subscribersPreview=(TextView) findViewById(R.id.textView3);
ApiInterface service = ApiClient.getClient().create(ApiInterface.class);
Call<Mainjson> call = service.getMainJson();
call.enqueue(new Callback<Mainjson>() {
@Override
public void onResponse(Call<Mainjson> call, Response<Mainjson> response) {
List<Items> items = response.body().getItems();
statistics=items.get(0).getStatistics();
subscribers=statistics.getSubscriberCount();
subscribersPreview.setText(subscribers);
}
@Override
public void onFailure(Call<Mainjson> call, Throwable t) {
Toast.makeText(MainActivity.this,"Failed to retrieve data",Toast.LENGTH_SHORT).show();
}
});
}
}
The retrofit instance :
public class ApiClient {
public static final String BASE_URL = "https://www.googleapis.com/youtube/v3";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}