I am implementing an Android application using Retrofit 2, the service is generic the server side is working.
The problem is: The response.body () is always null, I can never get json, even though I can print it with OKHTTP.
How to solve this problem or get the json 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);
(...)
}