如何将日志消息中的呼叫响应打印成json格式

时间:2017-11-13 04:54:20

标签: android retrofit2 android-json gson

我使用了改装2.0来进行服务器调用,因为它消耗了json并发送了json。我正在使用状态代码获得返回响应。但是,我的值没有插入服务器。我认为我的json数据错了。但是,如何打印我们的客户端json意味着在Log.d()中调用Callrespose。我试过但没有解决方案。 Main.java:

 ApiConfig getResponse = AppConfig.getRetrofit().create(ApiConfig.class);
        UserDTO userDTO=new UserDTO();
        userDTO.setUserid(userId);
        userDTO.setFile(file);
        userDTO.setUsers(adapter.usersList);

       Call<UserDTO> response = getResponse.ShareFiles(userDTO);

        Log.d("Shared Files : ",userDTO.toString());

        response.enqueue(new Callback<UserDTO>() {
            @Override
            public void onResponse(Call<UserDTO> call, Response<UserDTO> response) {
                Log.d("Shared Friends Responce call : ", String.valueOf(call.isExecuted())+"\n"+call.toString()+"\n"+call.clone());

                Log.d("Shared Friends Responce : ",response.message()+" "+response.code());

            }

            @Override
            public void onFailure(Call<UserDTO> call, Throwable t) {
                Log.d("Shared Friends Error : ",t.getMessage());
            }
        });

API配置:

public class AppConfig {
private static String BASE_URL = "http://104.236.67.117:8000/";
    static Retrofit getRetrofit() {
        return new Retrofit.Builder()
                .baseUrl(AppConfig.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
}

1 个答案:

答案 0 :(得分:0)

找到解决方案

第1步:    确定HTTP在Build.gradle中添加依赖项

    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    compile 'com.squareup.okhttp3:okhttp:3.4.1'

第2步:在Appconfig中

 HttpLoggingInterceptor logging = new HttpLoggingInterceptor();  
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();   
    httpClient.addInterceptor(logging); // this is for log interceptor
    Retrofit retrofit = new Retrofit.Builder()  
    .baseUrl(API_BASE_URL)
    .addConverterFactory(GsonConverterFactory.create())
    .client(httpClient.build())
    .build();