I struggled with this problem in the last hours and I looked for a guide.
I have an update request that I want to write in a generic way, which means that I would like to get a JSON of any kind, like a bag of property, and be able to work with it later in the application.
The problem I'm facing is that whenever I try to get it as JSON or String (and not as Model or ResponseBody) the app crashes. I think the arrest is due to decoding based on what I see.
This is the Retrofit builder.
public Retrofit getRetrofit(OkHttpClient okHttpClient) {
Retrofit retrofit = new Retrofit
.Builder()
.baseUrl("https://reqres.in/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(MoshiConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
return retrofit;
}
The REST interface is super simple.
public interface IRestService {
@GET("api/users?page=2")
Observable<String> queryPage();
}
I also tried to debug this issue, and I added my logger interceptor, that obviously does not work, and I have no idea why:
public OkHttpClient getOkHttpClient(Interceptor requestInterceptor) {
// HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
// loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
HttpLoggingInterceptor loggingInterceptor =
new HttpLoggingInterceptor((msg) -> {
Log.d(HttpLoggingInterceptor.class.toString(), msg);
});
//loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
LoggingInterceptor customLoggingInterceptor = new LoggingInterceptor();
return new okhttp3.OkHttpClient.Builder()
.connectTimeout(CONNECT_TIMEOUT_IN_MS, TimeUnit.MILLISECONDS)
.addInterceptor(requestInterceptor)
.addInterceptor(loggingInterceptor)
.addInterceptor(customLoggingInterceptor)
.build();
}
Please let me know if there is anything I am doing wrong in getting the JSON out in a generic form, without a model. And it would also be a great help if I could get some advice on how to debug this kind of problem, since the logger did not register anything in Android Studio. Sorry if none of these questions is silly, but I've just started building my first app in Android. I have experience in many other languages.