笔计:okhttp3 Interceptor 拦截器 请求后

public class DataInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Response response = chain.proceed(request);
        String context = response.body().string();
        String responseBody = "";
        try {
            JSONObject object = new JSONObject(context);
            String result = object.getString("result");
            if(result != null && !result.equals("") && !result.equals("null")){
                if(result.equals("ok")){
                    responseBody = "{\"status\":\""+object.getString("status")+"\",\"message\":\""+object.getString("message")+"\",\"data\":{\"ok\":\"ok\"}}";
                }else{
                    String decryptData = CDESCrypt.decryptString(result, AppConstant.APP_KEY);
                    responseBody = "{\"status\":\""+object.getString("status")+"\",\"message\":\""+object.getString("message")+"\",\"data\":"+decryptData+"}";
                }
            }else{
                responseBody = "{\"status\":\""+object.getString("status")+"\",\"message\":\""+object.getString("message")+"\",\"data\":null}";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        MediaType mediaType = response.body().contentType();
        return response.newBuilder().body(ResponseBody.create(mediaType, responseBody)).build();
    }
}

你可能感兴趣的:(笔计:okhttp3 Interceptor 拦截器 请求后)