The answer is Null when I use the JsonObject in modification

When I use JsonObject as the retrofit response, my output is: response: {}

Here is my code with JsonObject response:

mCall=apiService.Check_App_Version("api/check-app-version/1/"+Utility.Get_App_Version(context));
        mCall.enqueue(new Callback<JSONObject>() {
            @Override
            public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
                Log.e("response",""+response.body().toString()+" "+response.code());

            }
            @Override
            public void onFailure(Call<JSONObject> call, Throwable t) {
                Log.e("ERROR",t.toString());
            }
        });

      

But when I use Object as my retrofit answer, my output is:

response: {data=[{result=1.0, is_necessary=0.0}]}

      

and here is the code for that:

mCall=apiService.Check_App_Version("api/check-app-version/1/"+Utility.Get_App_Version(context));
        mCall.enqueue(new Callback<Object>() {
            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {
                Log.e("response",""+response.body().toString()+" "+response.code());

            }
            @Override
            public void onFailure(Call<Object> call, Throwable t) {
                Log.e("ERROR",t.toString());
            }
        });

      

I want to use it as JsonObject. where is my mistake?

+3


source to share


1 answer


Please use GsonConverterFactory to get the parsed response in the desired object, and if you want to get the String response using Retrofit use ScalarsConverterFactory. You can add both factories at the same time.



OkHttpClient client = new OkHttpClient.Builder().connectTimeout(3, TimeUnit.MINUTES)
            .writeTimeout(3, TimeUnit.MINUTES)
            .readTimeout(3, TimeUnit.MINUTES).addInterceptor(interceptor).build();

    if (retrofit == null) {
        String baseUrl = "http://example.com/";
        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .client(client)
                .build();
    }

      

0


source







All Articles