HttpLoggingInterceptor for http request & response logging

前端 未结 3 939
后悔当初
后悔当初 2021-01-13 19:37

I\'m using retrofit2 and I need to log all request and response. Request and response works perfectly, All I need is to log those request/response, I tried almost every solu

3条回答
  •  抹茶落季
    2021-01-13 19:55

    Make API call like this.

    ApiFactory.java

    public class ApiFactory {
    
    /**
     * Base URL for API calls
     */
    private static final String BASE_URL = "";
    
    public ApiFactory() {
    }
    
    private static Retrofit provideRestAdapter() {
    
        return new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(BaseApplication.getInstance().getOkHttpClient())
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }
    
    public static  S createService(Class serviceClass) {
        return provideRestAdapter().create(serviceClass);
    }
    

    }

    LoginService Interface

    public interface LoginService {
    
    /**
     * To Post FormUrlEncoded to web service
     *
     * @return Call Object of Type JsonObject
     */
    
    @FormUrlEncoded
    @POST("api/login")
    Call login(@Field("email") String email,
                           @Field("password") String password,
                           @Field("devicetype") String devicetype,
                           @Field("deviceid") String deviceid);
    
    }
    

    Make API call here

    private void emailLoginRequest() {
        LoginService loginService = ApiFactory.createService(LoginService.class);
        Call call = loginService.login(edtEmail.getText().toString(),edtPassword.getText().toString(),mDeviceType,mDeviceToken);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                hideProgressDialog();
                if (response.isSuccessful()) {
                    LOGD(TAG, "onResponse 0: " + response.body().toString());
                    LoginResponse loginResponse = new Gson().fromJson(response.body().toString(), LoginResponse.class);
    
                    System.out.println("+++ get message >> " + loginResponse.getMessage());
                    int status = loginResponse.getStatus();
    
                }else {
                    LOGD(TAG, "response fail 0: " + response.body());
                }
            }
    
            @Override
            public void onFailure(Call call, Throwable t) {
                hideProgressDialog();
                LOGD(TAG, "onFailure: " + t.getMessage());
            }
        });
    }
    

    LoginResponse Make changes as per yours.

    public class LoginResponse {
    
    @SerializedName("status")
    @Expose
    private Integer status;
    @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("data")
    @Expose
    private Data data;
    
    /**
     * No args constructor for use in serialization
     *
     */
    public LoginResponse() {
    
    Sample response model
                //        {
        //            "status": 1,
        //                "data": {
        //            "user_id": "565464564",
        //                    "email": "email@email.com",
        //                    "fullname": "james",
        //                    "username": "james123",
        //                    "country": "54654654",
        //                    "city": "56546465546",
        //                    "token": "dfgdfgdfg545465465464564"
        //        },
        //            "message": "Login successfull"
        //        }
    }
    
    /**
     *
     * @param message
     * @param status
     * @param data
     */
    public LoginResponse(Integer status, String message, Data data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }
    
    /**
     *
     * @return
     * The status
     */
    public Integer getStatus() {
        return status;
    }
    
    /**
     *
     * @param status
     * The status
     */
    public void setStatus(Integer status) {
        this.status = status;
    }
    
    /**
     *
     * @return
     * The message
     */
    public String getMessage() {
        return message;
    }
    
    /**
     *
     * @param message
     * The message
     */
    public void setMessage(String message) {
        this.message = message;
    }
    
    /**
     * @return The data
     */
    public Data getData() {
        return data;
    }
    
    /**
     * @param data The data
     */
    public void setData(Data data) {
        this.data = data;
    }
    
    public class Data {
    
        @SerializedName("user_id")
        @Expose
        private String userId;
    
        @SerializedName("email")
        @Expose
        private String email;
    
        /**
         * No args constructor for use in serialization
         */
        public Data() {
        }
    
        /**
         * @param email
         * @param userId
         */
        public Data(String userId, String email) {
            this.userId = userId;
            this.email = email;
        }
    
        /**
         * @return The userId
         */
        public String getUserId() {
            return userId;
        }
    
        /**
         * @param userId The user_id
         */
        public void setUserId(String userId) {
            this.userId = userId;
        }
    
        /**
         * @return The email
         */
        public String getEmail() {
            return email;
        }
    
        /**
         * @param email The email
         */
        public void setEmail(String email) {
            this.email = email;
        }
    
    }
    }
    

    Enjoy!

提交回复
热议问题