Retrofit, Response body always null but on the server is status 200

Asked

Viewed 303 times

1

I am implementing an Android application using Retrofit 2, the service is generic the server side is working.

The problem is: Response.body() is always null, I can never catch json, even if I can print it with OKHTTP.

How to solve this problem or get json straight from okhttp?

Servicegenerator

public class ServiceGenerator {

    public static <S> S createService(Class<S> serviceClass) {

        //Instancia do interceptador das requisições
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
                .readTimeout(15, TimeUnit.SECONDS);

        httpClient.addInterceptor(loggingInterceptor);


        //Instância do retrofit
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(API_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(new Gson()))
                .client(httpClient.build())
                .build();

        return retrofit.create(serviceClass);
    }
}

Retrofirservicespost

public interface RetrofitServicePost {

    @FormUrlEncoded
    @GET("tipoalerta")
    Call<List<TipoAlerta>> getListaTipoAlerta();

    @GET("tipoalerta/id/{id}")
    Call<TipoAlerta> getTipoAlerta(@Path("id") String id);

(...)
}

1 answer

0


I found the answer, it was a simple mistake. The Servivegenerator was built to wait as a response a list, just change the construction of the request and everything worked correctly.

Retrofitservices

    public interface RetrofitServicePost {

        @FormUrlEncoded
        @GET("tipoalerta")
        Call<List<TipoAlerta>> getListaTipoAlerta();

        @FormUrlEncoded
        @GET("tipoalerta/id/{id}")
        Call<List<TipoAlerta>> getTipoAlerta(@Path("id") String id);

    (...)
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.