|
@@ -1,4 +1,4 @@
|
|
|
-package com.benyanyi.okhttp.type;
|
|
|
+package com.benyanyi.okhttp.config;
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
@@ -6,12 +6,12 @@ import com.benyanyi.okhttp.call.HttpCall;
|
|
|
import com.benyanyi.okhttp.call.RequestConfig;
|
|
|
import com.benyanyi.okhttp.download.DownloadCall;
|
|
|
import com.benyanyi.okhttp.download.DownloadConfig;
|
|
|
+import com.benyanyi.okhttp.type.RequestType;
|
|
|
import com.benyanyi.okhttp.util.FormatUtil;
|
|
|
import com.benyanyi.okhttp.util.OkHttpLog;
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
|
import java.io.File;
|
|
|
-import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import okhttp3.FormBody;
|
|
@@ -83,40 +83,23 @@ public class HttpRequest implements RequestType {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public RequestConfig postFileText(Map<String, Object> bodyMap) {
|
|
|
+ public RequestConfig postMultipart(Map<String, Object> headerMap, Map<String, Object> bodyMap) {
|
|
|
MultipartBody.Builder builder = new MultipartBody.Builder()
|
|
|
.setType(MultipartBody.FORM);
|
|
|
if (FormatUtil.isMapNotEmpty(bodyMap)) {
|
|
|
for (Map.Entry<String, Object> entry : bodyMap.entrySet()) {
|
|
|
if (entry.getValue() instanceof File) {
|
|
|
- File file = new File(entry.getValue().toString());
|
|
|
+ File file = (File) entry.getValue();
|
|
|
RequestBody requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
|
|
|
builder.addFormDataPart(entry.getKey(), file.getName(), requestBody);
|
|
|
} else {
|
|
|
- builder.addFormDataPart(entry.getKey(), entry.getValue().toString());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- MultipartBody multipartBody = builder.build();
|
|
|
- request = new Request.Builder()
|
|
|
- .url(url)
|
|
|
- .post(multipartBody)
|
|
|
- .build();
|
|
|
- return send(true);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public RequestConfig postFileText(Map<String, Object> headerMap, Map<String, Object> bodyMap) {
|
|
|
- MultipartBody.Builder builder = new MultipartBody.Builder()
|
|
|
- .setType(MultipartBody.FORM);
|
|
|
- if (FormatUtil.isMapNotEmpty(bodyMap)) {
|
|
|
- for (Map.Entry<String, Object> entry : bodyMap.entrySet()) {
|
|
|
- if (entry.getValue() instanceof File) {
|
|
|
File file = new File(entry.getValue().toString());
|
|
|
- RequestBody requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
|
|
|
- builder.addFormDataPart(entry.getKey(), file.getName(), requestBody);
|
|
|
- } else {
|
|
|
- builder.addFormDataPart(entry.getKey(), entry.getValue().toString());
|
|
|
+ if (file.exists()) {//判断是否为文件路径
|
|
|
+ RequestBody requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
|
|
|
+ builder.addFormDataPart(entry.getKey(), file.getName(), requestBody);
|
|
|
+ } else {
|
|
|
+ builder.addFormDataPart(entry.getKey(), entry.getValue().toString());
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -135,18 +118,23 @@ public class HttpRequest implements RequestType {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public RequestConfig postJson(Map<String, Object> map) {
|
|
|
+ public RequestConfig postJson(Map<String, String> headerMap, Map<String, Object> map) {
|
|
|
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
|
|
|
+ Request.Builder builder = new Request.Builder();
|
|
|
+ if (FormatUtil.isMapNotEmpty(headerMap)) {
|
|
|
+ for (Map.Entry<String, String> entry : headerMap.entrySet()) {
|
|
|
+ builder.addHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
if (FormatUtil.isMapNotEmpty(map)) {
|
|
|
mCacheName = mCacheName + map;
|
|
|
RequestBody requestBody = RequestBody.create(new Gson().toJson(map), mediaType);
|
|
|
- request = new Request.Builder()
|
|
|
+ request = builder
|
|
|
.url(url)
|
|
|
- .addHeader("content-type", "application/json")
|
|
|
.post(requestBody)
|
|
|
.build();
|
|
|
} else {
|
|
|
- request = new Request.Builder()
|
|
|
+ request = builder
|
|
|
.url(url)
|
|
|
.post(new FormBody.Builder().build())
|
|
|
.build();
|
|
@@ -155,18 +143,23 @@ public class HttpRequest implements RequestType {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public RequestConfig postJson(String string) {
|
|
|
+ public RequestConfig postJson(Map<String, String> headerMap, String jsonBody) {
|
|
|
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
|
|
|
- if (FormatUtil.isNotEmpty(string)) {
|
|
|
- mCacheName = mCacheName + string;
|
|
|
- RequestBody requestBody = RequestBody.create(string, mediaType);
|
|
|
- request = new Request.Builder()
|
|
|
+ Request.Builder builder = new Request.Builder();
|
|
|
+ if (FormatUtil.isMapNotEmpty(headerMap)) {
|
|
|
+ for (Map.Entry<String, String> entry : headerMap.entrySet()) {
|
|
|
+ builder.addHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (FormatUtil.isNotEmpty(jsonBody)) {
|
|
|
+ mCacheName = mCacheName + jsonBody;
|
|
|
+ RequestBody requestBody = RequestBody.create(jsonBody, mediaType);
|
|
|
+ request = builder
|
|
|
.url(url)
|
|
|
- .addHeader("content-type", "application/json")
|
|
|
.post(requestBody)
|
|
|
.build();
|
|
|
} else {
|
|
|
- request = new Request.Builder()
|
|
|
+ request = builder
|
|
|
.url(url)
|
|
|
.post(new FormBody.Builder().build())
|
|
|
.build();
|
|
@@ -175,88 +168,25 @@ public class HttpRequest implements RequestType {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public RequestConfig xml(String xml) {
|
|
|
- String str = xml;
|
|
|
+ public RequestConfig xml(Map<String, String> headerMap, String xmlBody) {
|
|
|
+ String str = xmlBody;
|
|
|
MediaType mediaType = MediaType.parse("text/xml; charset=UTF-8");
|
|
|
if (FormatUtil.isNotEmpty(str)) {
|
|
|
mCacheName = mCacheName + str;
|
|
|
} else {
|
|
|
str = "";
|
|
|
}
|
|
|
- request = new Request.Builder()
|
|
|
- .url(url)
|
|
|
- .post(RequestBody.create(str, mediaType))
|
|
|
- .build();
|
|
|
- return send();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public RequestConfig file(Map<String, Object> map) {
|
|
|
- MultipartBody.Builder builder = new MultipartBody.Builder()
|
|
|
- .setType(MultipartBody.FORM);
|
|
|
- if (FormatUtil.isMapNotEmpty(map)) {
|
|
|
- for (Map.Entry<String, Object> entry : map.entrySet()) {
|
|
|
- File file = new File(entry.getValue().toString());
|
|
|
- RequestBody requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
|
|
|
- builder.addFormDataPart(entry.getKey(), file.getName(), requestBody);
|
|
|
- }
|
|
|
- }
|
|
|
- MultipartBody multipartBody = builder.build();
|
|
|
- request = new Request.Builder()
|
|
|
- .url(url)
|
|
|
- .post(multipartBody)
|
|
|
- .build();
|
|
|
- return send(true);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public RequestConfig file(String... filePaths) {
|
|
|
- MultipartBody.Builder builder = new MultipartBody.Builder()
|
|
|
- .setType(MultipartBody.FORM);
|
|
|
- if (filePaths != null && filePaths.length > 0) {
|
|
|
- if (filePaths.length == 1) {
|
|
|
- File file = new File(filePaths[0]);
|
|
|
- RequestBody requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
|
|
|
- builder.addFormDataPart("file", file.getName(), requestBody);
|
|
|
- } else {
|
|
|
- for (int i = 0; i < filePaths.length; i++) {
|
|
|
- File file = new File(filePaths[i]);
|
|
|
- RequestBody requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
|
|
|
- builder.addFormDataPart("file" + i, file.getName(), requestBody);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- MultipartBody multipartBody = builder.build();
|
|
|
- request = new Request.Builder()
|
|
|
- .url(url)
|
|
|
- .post(multipartBody)
|
|
|
- .build();
|
|
|
- return send(true);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public RequestConfig file(List<String> filePaths) {
|
|
|
- MultipartBody.Builder builder = new MultipartBody.Builder()
|
|
|
- .setType(MultipartBody.FORM);
|
|
|
- if (filePaths != null && filePaths.size() > 0) {
|
|
|
- if (filePaths.size() == 1) {
|
|
|
- File file = new File(filePaths.get(0));
|
|
|
- RequestBody requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
|
|
|
- builder.addFormDataPart("file", file.getName(), requestBody);
|
|
|
- } else {
|
|
|
- for (int i = 0; i < filePaths.size(); i++) {
|
|
|
- File file = new File(filePaths.get(i));
|
|
|
- RequestBody requestBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
|
|
|
- builder.addFormDataPart("file" + i, file.getName(), requestBody);
|
|
|
- }
|
|
|
+ Request.Builder builder = new Request.Builder();
|
|
|
+ if (FormatUtil.isMapNotEmpty(headerMap)) {
|
|
|
+ for (Map.Entry<String, String> entry : headerMap.entrySet()) {
|
|
|
+ builder.addHeader(entry.getKey(), entry.getValue());
|
|
|
}
|
|
|
}
|
|
|
- MultipartBody multipartBody = builder.build();
|
|
|
- request = new Request.Builder()
|
|
|
+ request = builder
|
|
|
.url(url)
|
|
|
- .post(multipartBody)
|
|
|
+ .post(RequestBody.create(xmlBody, mediaType))
|
|
|
.build();
|
|
|
- return send(true);
|
|
|
+ return send();
|
|
|
}
|
|
|
|
|
|
@Override
|