zoukankan      html  css  js  c++  java
  • 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();
                        }
                    }
  • 相关阅读:
    数组变成地址栏参数函数
    Excel导出生成多个sheet php
    重置linux里mysql的密码,通过修改配置文件
    小程序中把对象转化成字符串
    linux中导出数据库中的表结构跟数据
    移动端点击事件兼容问题,在pc端可以点,在手机上不可以点
    微信获取token
    uat
    实验报告 四
    Pikachu-File Inclusion, Unsafe file download & Unsafe file upload
  • 原文地址:https://www.cnblogs.com/liyanli-mu640065/p/9875282.html
Copyright © 2011-2022 走看看