0

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.

Shashanth
  • 4,995
  • 7
  • 41
  • 51
  • Share that code too where you calling IRestService#queryPage. I think to do that you need to write your own JsonDeserializer – Krishna Sharma Sep 19 '18 at 07:28

1 Answers1

1

Instead of using String for your Observable in the service(interface), use Object as the type. And when you get the data, you can always use Gson().toJson(body.toString()) to convert it to the string. Also, most probably your app is crashing for String because you don't have the correct converter factory (Scalar Factory). You can check this for the same.

karandeep singh
  • 2,294
  • 1
  • 15
  • 22
  • I will try with Object as well. I already tried with JSONObject and Gson for the exact reason, ConverterFactory, that you mentioned. Is any way to catch the exception at the app level, because right now the serialization is crashing in C++ runtime. –  Sep 19 '18 at 15:21
  • Also, thanks a lot for the link! It is super useful. –  Sep 19 '18 at 15:26
  • You can catch the exception at the app level, but I think using Object should solve your issue because I checked it before posting the answer. – karandeep singh Sep 19 '18 at 16:30
  • Thanks a lot karandeep! I was fighting 2 issues in the same time. I had the crash that was actually caused by a breakpoint in an inline function (ie, https://stackoverflow.com/questions/40618803/android-app-crashes-when-launched-in-debug-mode) and this error confused me a lot, as I did not expect such things and mixed things with the trauma caused by the editor/debugger. –  Sep 20 '18 at 06:29