• Retrofit 打印log时,中文会显示类似%E8%BE%BD字符


    https://blog.csdn.net/honghailiang888/article/details/54289632?utm_source=blogxgwz6

    参照Android Retrofit2.1.0设置编码格式GBK 的文章。

    OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
            /***lyl TODO 下面一行代码加入https的接口访问**/
            //builder.sslSocketFactory(getSSLSocketFactory(context));
            builder.addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Interceptor.Chain chain) throws IOException {
                    Request original = chain.request();
                // Request customization: add request headers
    //            Log.d(TAG, fdata);StringUtil.encryptAsDoNet(fdata, 1)
    //            Log.d(TAG, "加密后:"+DESUtils.encryptAsDoNet(fdata, 1));
                    Request.Builder requestBuilder = null;
                    try {
                        requestBuilder = original.newBuilder()
                                .addHeader("androidorios", "android");
                    } catch (Exception e) {
                        LogUtil.e(TAG,"---create--addhead---"+e.toString());
                    }
                    Request request = requestBuilder.build();
                    request = requestBuilder.post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded;charset=utf-8"),
                            URLDecoder.decode(bodyToString(request.body()), "UTF-8")))
                            .build();
                    return chain.proceed(request);
            }
            });

    就可以解决问题。

    2018年11月27日15:32:36

    今日我们产品在测试时,发现有个接口提示请求失败。我log显示:

    ---onError--Throwable-java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String okhttp3.MediaType.toString()' on a null object reference

    首先想到除了这次改动,其他没涉及到。查看了自己这个接口恰恰:传递的参数是空的。所以更能判断是自己这里少加了非空的判断。修改后的代码如下:

    Request request = requestBuilder.build();
                    if(null!=request&&request.body().contentLength()>0) {
                        LogUtil.d(UrlConfig.OkHttp, "---request.body.type--" + request.body().contentType().toString());
                        if ((request.body().contentType().toString()).equals("application/x-www-form-urlencoded")) {
                            request = requestBuilder.post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded;charset=utf-8"),
                                    URLDecoder.decode(bodyToString(request.body()), "UTF-8")))
                                    .build();
                        }
                    }
  • 相关阅读:
    四则运算测试脚本运行情况
    AAA
    (2015秋) 软工作业成绩公布(12月26号更新)
    判断闰年的Java算法
    B
    A
    Where Amazing Happens
    安利一发资料站
    C
    B
  • 原文地址:https://www.cnblogs.com/liyanli-mu640065/p/9875282.html
Copyright © 2020-2023  润新知