Возникли сложности с формированием запроса API с помощью retrofit?
Вот так выглядит класс API:
public class API
{
private static final String SOURCE = "http://moonwalk.cc/api";
private final CinemaService mService;
String api_token="тут токен";
final String TAG = "myLogs";
Context ctx;
ArrayList<Cinema> cinemas = new ArrayList<Cinema>();
public interface CinemaService {
@GET("/serial_updates.json")
Call<CinemaData> getCinemas(@Query("api_token") String api_token);
}
public interface APIS
{
public void getData();
}
APIS apis;
public API(Context ctx) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SOURCE)
.addConverterFactory(GsonConverterFactory.create())
.build();
mService = retrofit.create(CinemaService.class);
try {
apis = (APIS) ctx;
} catch (ClassCastException e) {
throw new ClassCastException(ctx.toString()
+ " must implement onSomeEventListener");
}
this.ctx = ctx;
}
public boolean getCinema(final int numCat, final String title, final int kinopoisk_id)
{
mService.getCinemas(api_token).enqueue(new Callback<CinemaData>() {
@Override
public void onResponse(Response<CinemaData> response, Retrofit retrofit) {
//cinemas.add(new Cinema(response.body().responseData.current_page));
Log.d("myLogs", "s="+response.body().responseData.serial);
apis.getData();
}
@Override
public void onFailure(Throwable t) {
Log.d("myLogs", "Failed!!!");
}
});
return true;
}
}
class CinemaData {
ResponseData responseData;
}
class ResponseData {
String serial;
По такому адресу осуществляется запрос:
http://moonwalk.cc/api/serials_updates.json?api_token=тут токен
вывод json выглядит так:
{"current_page":1,"updates":[{"added_at":"2016-02-22 19:12:17","token":"222db177654acd99","episode_iframe_url":"http://moonwalk.cc/serial/d3e8cd59ad3790aa5ff2393058014e1a/iframe?episode=4\u0026season=1","video_iframe_url":"http://moonwalk.cc/video/222db177654acd99/iframe","serial":{"title_ru":"Путь к выздоровлению","title_en":"Recovery Road","token":"d3e8cd59ad3790aa5ff2393058014e1a","type":"serial","kinopoisk_id":891572,"translator":"HamsterStudio ","translator_id":8,"iframe_url":"http://moonwalk.cc/serial/d3e8cd59ad3790aa5ff2393058014e1a/iframe","seasons_count":1,"episodes_count":4,"category":null}},{"added_at":"2016-02-22 18:34:50","token":"59ad4ac760fd5f52","episode_iframe_url":"http://moonwalk.cc/serial/befca8d16d813d73aab573ea39545be3/iframe?episode=7\u0026season=1","video_iframe_url":"http://moonwalk.cc/video/59ad4ac760fd5f52/iframe","serial":{"title_ru":"Отвечай-ка, наша Галко!","title_en":"Oshiete! Galko-chan","token":"befca8d16d813d73aab573ea39545be3","type":"serial","kinopoisk_id":null,"translator":"AniDUB","translator_id":49,"iframe_url":"http://moonwalk.cc/serial/befca8d16d813d73aab573ea39545be3/iframe","seasons_count":1,"episodes_count":7,"category":"anime"}},
Как из такого вывода получить данные поля "title_ru", я так понимаю перед этим надо получить данные поля "serial"?!
Приведенный код выдает ошибку:
java.lang.NullPointerException: Attempt to read from field 'net.kinomovies.onlinemovielibrary.ResponseData net.kinomovies.onlinemovielibrary.CinemaData.responseData' on a null object reference