【问题标题】:How to print call responce in Log message into json format如何将日志消息中的呼叫响应打印为 json 格式
【发布时间】:2023-03-11 07:05:01
【问题描述】:

我使用改造 2.0 进行服务器调用,因为消耗 json 并发送 json。我收到状态码的回复。但是,我的值没有插入服务器。我认为我的 json 数据错误。但是,我如何打印我们的客户端 json 意味着 Log.d() 中的 Callrespose。我试过但没有解决办法。 主.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();
    }
}

【问题讨论】:

    标签: android retrofit2 android-json gson


    【解决方案1】:

    找到解决办法

    第 1 步: OK 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();
    

    【讨论】:

    • 我可以这样使用:logging.d("Main activity", response);其中响应是调用response.
    • 这是打印日志级别正文的方式。在这里您将得到响应的简单方法来查找异常或任何内容
    • 它帮助满满。你节省了我的时间。谢谢@Rajasekhar Reddy
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 2014-05-24
    相关资源
    最近更新 更多