5

I am receiving a body from my API call but onResponse() is not getting called, here are the methods:

final Rest_manager_league rest = new Rest_manager_league();
Call<List<Root>> listCall = rest.getMLeague_conn().getLeague(x);
listCall.enqueue(new Callback<List<Root>>() {
    @Override
    public void onResponse(Call<List<Root>> call, Response<List<Root>> response) {
        lg = response.body();
        Log.d("res", "ON");
        if (response.isSuccessful()){
            textView.setText(lg.get(3).getStanding().get(2).getTeamName());
            Log.d("s", "true");
        }
    }

    @Override
    public void onFailure(Call<List<Root>> call, Throwable t) {
        Log.d("Failure", "Failed");
    }
});

Here is the Retrofit interface & the service:

public interface league_Conn {
    @GET("/v1/soccerseasons/{id}/leagueTable")
    @Headers("X-Auth-Token:" +
            "1869f69f772b40a2a12fd6eefb4e48ef ")
    Call<List<Root>> getLeague(@Path("id") int id);
}

public class Rest_manager_league {
    private league_Conn mleague_conn;

    public league_Conn getMLeague_conn() {

        if (mleague_conn == null) {

            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient client = new OkHttpClient.Builder().addInterceptor(logging).build();
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("http://api.football-data.org/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(client)
                    .build();
            mleague_conn = retrofit.create(league_Conn.class);

        }


        return mleague_conn;
    }
}

In the logcat, onFailure() is showing up. Like so:

okhttp3 <-- END HTTP (8300 byte body) Failer :Failed

Why is onResponse() not getting called?

Daniel
  • 2,355
  • 9
  • 23
  • 30
Hassan Raza
  • 85
  • 2
  • 7

1 Answers1

4

You are getting a response body (8300 bytes) but onFailure is getting called, because your returned body does not agree with your GSONFactory. The deserialization process did not work. You can pinpoint the problem by printing a stack trace as @yazan pointed out. Just type:

t.printStackTrace()

in onFailure().

Edit:

The error occurs because you're telling Retrofit that you're expecting a list but instead you're getting a JSON object. I took a quick look at the API that you're using and it looks like it returns a JSON object and the returned object then contains the list you're interested in accessing. Try replacing instances of List<Root> to just Root. For more help, you can also check this question:

GSON throws Expected BEGIN_ARRAY but was BEGIN_OBJECT error

Community
  • 1
  • 1
Daniel
  • 2,355
  • 9
  • 23
  • 30
  • W/System.err: java.lang.IllegalStateException:ExpectedBEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ 05-16 20:47:17.782 23099-23099/com.example.dell.project_practise4 W/System.err:at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350) 05-16 20:47:17.782 23099-23099/com.example.dell.project_practise4 W/System.err: at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80) 05-16 20:47:17.782 23099-23099/com.example.dell.project_practise4 – Hassan Raza May 16 '16 at 15:50
  • Yes you are absolutely right above this tracer solved my problem almost. – Hassan Raza May 16 '16 at 18:39
  • When I ran into this problem, I had to do a lot of searching to discover the source. So in order to help fellow devs find this answer quickly, I am suggesting that you change the question title to something more descriptive like "Receiving response body in Retrofit2 but onResponse is not getting called". It's pretty simple once you know that onFailure gets called because the deserialization did not work. Finding that piece of information though, took me quite a bit of searching. – Daniel May 17 '16 at 14:31