|
@@ -1,11 +1,16 @@
|
|
|
package com.cooleshow.base.data.net;
|
|
|
|
|
|
+import android.text.TextUtils;
|
|
|
+
|
|
|
+import com.cooleshow.base.utils.RequestBodyUtil;
|
|
|
+
|
|
|
import java.io.IOException;
|
|
|
|
|
|
import okhttp3.Headers;
|
|
|
import okhttp3.HttpUrl;
|
|
|
import okhttp3.Interceptor;
|
|
|
import okhttp3.Request;
|
|
|
+import okhttp3.RequestBody;
|
|
|
import okhttp3.Response;
|
|
|
|
|
|
/**
|
|
@@ -24,10 +29,30 @@ public class CommonInterceptor implements Interceptor {
|
|
|
|
|
|
// 新的请求,添加公共header参数
|
|
|
Request.Builder builder = oldRequest.newBuilder()
|
|
|
- .method(oldRequest.method(), oldRequest.body())
|
|
|
+ .method(oldRequest.method(), getBody(oldRequest))
|
|
|
.url(authorizedUrlBuilder.build());
|
|
|
CommonParamsHelper.getInstance().insertCommonHeaderParams(builder);
|
|
|
Request newRequest = builder.build();
|
|
|
return chain.proceed(newRequest);
|
|
|
}
|
|
|
+
|
|
|
+ private RequestBody getBody(Request oldRequest) {
|
|
|
+ String method = oldRequest.method();
|
|
|
+ if (TextUtils.equals(method, "POST")) {
|
|
|
+ RequestBody body = oldRequest.body();
|
|
|
+ try {
|
|
|
+
|
|
|
+ if (body == null || body.contentLength() == 0) {
|
|
|
+ String s = oldRequest.headers().get("Content-Type");
|
|
|
+ if (TextUtils.isEmpty(s)) {
|
|
|
+ //后端框架限制,POST请求的Content-type能不能为没有和body的内容不能为空内容
|
|
|
+ return RequestBodyUtil.convertToRequestBodyJson("{}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return oldRequest.body();
|
|
|
+ }
|
|
|
}
|