How To Get Retrofit Callback Get Response Body In Json Formate
I'm using Retrofit for integrating web services and when run my app controller going to Retrofit success block but how can i print ResponseBody in Json string formate can some one
Solution 1:
Try to enable logging mode by that u will be able to see the response, Here it is how its done,
app gradle
compile'com.squareup.retrofit2:retrofit:2.1.0'compile'com.squareup.retrofit2:converter-gson:2.1.0'compile'com.squareup.okhttp3:logging-interceptor:3.3.1'
build retrofit client like this,
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
HttpLoggingInterceptorlogging=newHttpLoggingInterceptor();
// set your desired log level
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
finalOkHttpClientokHttpClient=newOkHttpClient.Builder()
.readTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30, TimeUnit.SECONDS)
.addInterceptor(logging)
.build();
finalRetrofitretrofit=newRetrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
Solution 2:
Have you tested your API using postman or other API interceptors and checked what is the response?
If your api is giving proper response then response.body()
would give you an object in this case a List
i.e. you List of CardTypes. You need to convert this into a string to get it printed in proper format. You can use new Gson().toJson(object)
to convert your object to String
.
Post a Comment for "How To Get Retrofit Callback Get Response Body In Json Formate"