N
N
newdancer2016-02-22 19:41:51
Java
newdancer, 2016-02-22 19:41:51

Having trouble building an API request with retrofit?

Having trouble building an API request with retrofit?
This is what the API class looks like:

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;

Request is made to this address:
http://moonwalk.cc/api/serials_updates.json?api_token=тут токен

json output looks like this:
{"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"}},

How to get the data of the "title_ru" field from such an output, as I understand it, before that, you need to get the data of the "serial" field?!
The given code throws an error:
java.lang.NullPointerException: Attempt to read from field 'net.kinomovies.onlinemovielibrary.ResponseData net.kinomovies.onlinemovielibrary.CinemaData.responseData' on a null object reference

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2016-02-23
@zagayevskiy

Smoking what is GSON and how to work with it. There is no magic there. GSON looks in your response for the key responseData and doesn't find it, so responseData = null. Annotations must be correct. And your json doesn't match the structure you want to parse at all. Think about it.
And you, I see, do not learn anything, in the last question I gave you advice - and you did not follow them, post all the same shit-copy-paste. In vain.

G
Gregary, 2020-03-02
@Gregary

Here is an excellent tutorial in Russian and Kotlin on how to work with GSON and Retrofit androidschool.ru/courses/android-retrofit-and-gson

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question