解决Retrofit2上传中文乱码问题

  • 和服务器统一编码,基本上都是UTF-8
  • 给OKHTTP添加一个Interceptor(拦截器)
  • .addInterceptor(new Interceptor() {
    @Override
    public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    Request.Builder requestBuilder = request.newBuilder();
    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);
    }
    })

     

posted @ 2021-05-07 17:31  王哒哒  阅读(427)  评论(0编辑  收藏  举报