How to get Retrofit callback get response body in

2019-08-02 15:09发布

问题:

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 help me please

Main Activity:-

  import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

        @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                Retrofit retrofit = new Retrofit.Builder().baseUrl(url).
                        addConverterFactory(GsonConverterFactory.create()).build();

                RetrofitGetAPI service = retrofit.create(RetrofitGetAPI.class);

                retrofit.Call<List<CardTypes>> call = service.getProfileData();

                call.enqueue(new Callback<List<CardTypes>>() {
                    @Override
                    public void onResponse(Response<List<CardTypes>> response, Retrofit retrofit) {
                        System.out.println("Response is=====>" + response.body());
                    }

                    @Override
                    public void onFailure(Throwable t) {
                        System.out.println("Failure response is====>");
                    }
                });
            }

RetrofitGetAPI:- ------------

  public interface RetrofitGetAPI {

    @GET("api/RetrofitProfileResponse")
    Call<List<CardTypes>> getProfileData();
}

CardTypes:-

public class CardTypes {

    private int paymenttypeid;
    private String cardType;

    public String getCardType() {
        return cardType;
    }

    public void setCardType(String cardType) {
        this.cardType = cardType;
    }

    public int getPaymenttypeid() {
        return paymenttypeid;
    }

    public void setPaymenttypeid(int paymenttypeid) {
        this.paymenttypeid = paymenttypeid;
    }

}

Server response:-

   [
  {
    "paymenttypeid": 1,
    "cardType": "Master Card"
  },
  {
    "paymenttypeid": 2,
    "cardType": "VISA"
  }
]

回答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;

            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
            // set your desired log level
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);

            final OkHttpClient okHttpClient = new OkHttpClient.Builder()
                        .readTimeout(30, TimeUnit.SECONDS)
                        .connectTimeout(30, TimeUnit.SECONDS)
                        .addInterceptor(logging)
                        .build();

            final Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(url)
                        .addConverterFactory(GsonConverterFactory.create())
                        .client(okHttpClient)
                        .build();


回答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.