兰特斯 2021-01-22 11:35 采纳率: 0%
浏览 351

OKhttp拦截器requestBody类型判断失败

我用这边需要对上传到服务器的参数做修改。所以给OKHttp家了一个拦截器。但是在判断requestbody类型的时候遇到了问题,判断是否为FormBody的时候一直返回false,求问怎么解决。

请求:

 /**
     * post请求方式
     */
    @FormUrlEncoded //post请求必须要申明该注解
    @POST("login")   //方法名
    Observable<BaseReponse<LoginRequest>> login(@Field("username") String userName, @Field("password") String password);//请求参数

拦截器:

public class RequestInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Request.Builder builder = request.newBuilder();
        if (request.method().equals("POST")) {
            RequestBody requestBody = request.body();
            Loger.i(getClass().getName(),  request.body().getClass().getName());//这里打印出来是ContentTypeOverridingRequestBody
            if (requestBody instanceof FormBody) { //这里判断永远是false
                FormBody formBody = (FormBody) requestBody;
                LinkedHashMap<String, Object> hashMap = new LinkedHashMap<>();
                for (int i = 0; i < formBody.size(); i++) {
                    hashMap.put(formBody.encodedName(i), formBody.encodedValue(i));
                }
                builder.post(getRequestBody(hashMap));
            }
        }
        Log.v(getClass().getName(), "request:" + request.toString());
        Log.v(getClass().getName(), "request headers:" + request.headers().toString());
        return chain.proceed(builder.build());
    }

    public static RequestBody getRequestBody(HashMap<String,Object> hashMap){
        Gson gson = new Gson();
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=UTF-8"),gson.toJson(hashMap));

        return requestBody;
    }
}

拦截器调用:

  //初始化Okhttp,绑定拦截器事件
    OkHttpClient client = new OkHttpClient.Builder().
            connectTimeout(30, TimeUnit.SECONDS).                   //设置请求超时时间
            readTimeout(30, TimeUnit.SECONDS).                       //设置读取数据超时时间
            writeTimeout(30, TimeUnit.SECONDS)                      //设置写入数据超时时间
            .addInterceptor(new RequestInterceptor())       //绑定header拦截器
            .addNetworkInterceptor(InterceptorUtil.LogInterceptor() )
            .addInterceptor(InterceptorUtil.LogInterceptor())                //绑定日志拦截器
            .build();

    Retrofit retrofit = new Retrofit.Builder().
            addConverterFactory(GsonConverterFactory.create()).             //设置gson转换器,将返回的json数据转为实体
            addCallAdapterFactory(RxJava2CallAdapterFactory.create()).       //设置CallAdapter
            baseUrl(ApiHttpClient.getDefaultApiUrl()).
            client(client)                                                  //设置客户端okhttp相关参数
            .build();

debug的截图:

 

  • 写回答

3条回答 默认 最新

  • 雪中血狼 2021-04-13 16:30
    关注

    哥们,解决没有,我也遇到这个问题了

    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用