Commit 77949842 by 郑娜伟

net 发布1.1.5 支持put

common 发布1.2.3
parent 0f4802cd
...@@ -21,12 +21,12 @@ dependencies { ...@@ -21,12 +21,12 @@ dependencies {
// api 'com.sobot.library:utils:1.1.1' // api 'com.sobot.library:utils:1.1.1'
// api 'com.sobot.library:picture:1.1.4' // api 'com.sobot.library:picture:1.1.4'
// api 'com.sobot.library:net:1.1.3' // api 'com.sobot.library:net:1.1.5'
// api 'com.sobot.library:gson:1.1.1' // api 'com.sobot.library:gson:1.1.1'
// api 'com.sobot.library:utils:1.1.1' // api 'com.sobot.library:utils:1.1.1'
// api 'com.sobot.library:picture_x:1.1.4' // api 'com.sobot.library:picture_x:1.1.4'
// api 'com.sobot.library:net:1.1.3' // api 'com.sobot.library:net:1.1.5'
// api 'com.sobot.library:gson:1.1.1' // api 'com.sobot.library:gson:1.1.1'
} }
......
...@@ -13,7 +13,7 @@ ext { ...@@ -13,7 +13,7 @@ ext {
PUBLISH_GROUP_ID = "com.sobot.library" //项目包名 PUBLISH_GROUP_ID = "com.sobot.library" //项目包名
PUBLISH_ARTIFACT_ID = 'sobotcommon' //项目名 PUBLISH_ARTIFACT_ID = 'sobotcommon' //项目名
// PUBLISH_ARTIFACT_ID = 'sobotcommon_x' //项目名 // PUBLISH_ARTIFACT_ID = 'sobotcommon_x' //项目名
PUBLISH_VERSION = '1.2.2' //版本号 PUBLISH_VERSION = '1.2.3' //版本号
} }
......
...@@ -12,7 +12,7 @@ task androidSourcesJar(type: Jar) { ...@@ -12,7 +12,7 @@ task androidSourcesJar(type: Jar) {
ext { ext {
PUBLISH_GROUP_ID = "com.sobot.library" //项目包名 PUBLISH_GROUP_ID = "com.sobot.library" //项目包名
PUBLISH_ARTIFACT_ID = 'net' //项目名 PUBLISH_ARTIFACT_ID = 'net' //项目名
PUBLISH_VERSION = '1.1.3' //版本号 PUBLISH_VERSION = '1.1.5' //版本号
} }
......
...@@ -23,6 +23,7 @@ import java.util.Map; ...@@ -23,6 +23,7 @@ import java.util.Map;
import java.util.Random; import java.util.Random;
import okhttp3.Call; import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.MediaType; import okhttp3.MediaType;
import okhttp3.Response; import okhttp3.Response;
...@@ -211,6 +212,55 @@ public class HttpBaseUtils { ...@@ -211,6 +212,55 @@ public class HttpBaseUtils {
} }
} }
private static FormBody addParamToBuilder(Map<String, String> params) {
FormBody.Builder builder = new FormBody.Builder();
if (params != null && params.keySet().size() > 0) {
try {
for (String key : params.keySet()) {
builder.add(key, params.get(key));
}
} catch (Exception e) {
}
}
return builder.build();
}
public void doPut(Object cancelTag, final String url_str, Map<String, String> paramMap,
Map<String, String> headerMap,
final StringCallBack callback) {
SobotNetLogUtils.i("请求URL: --> " + url_str);
SobotNetLogUtils.i("请求参数: --> " + paramMap);
if (headerMap == null) {
headerMap = new HashMap<>();
}
FormBody formBody = addParamToBuilder(paramMap);
SobotOkHttpUtils
.put()
.requestBody(formBody)
.tag(cancelTag)
.url(url_str)
.headers(headerMap)
.build()
.readTimeOut(8 * 1000)
.writeTimeOut(8 * 1000)
.connTimeOut(8 * 1000).execute(new StringCallback() {
@Override
public void onResponse(String response) {
SobotNetLogUtils.i(url_str + "----请求返回结果: --> " + response);
callback.onResponse(response);
}
@Override
public void onError(Call call, Exception e) {
SobotNetLogUtils.i(call.toString());
e.printStackTrace();
callback.onError(e, call.toString(), -1);
}
});
}
public void download(String url_str, final File file, public void download(String url_str, final File file,
final FileCallBack callback) { final FileCallBack callback) {
SobotNetLogUtils.i("下载地址:" + url_str); SobotNetLogUtils.i("下载地址:" + url_str);
...@@ -444,16 +494,16 @@ public class HttpBaseUtils { ...@@ -444,16 +494,16 @@ public class HttpBaseUtils {
if (headerMap == null) { if (headerMap == null) {
headerMap = new HashMap<>(); headerMap = new HashMap<>();
} }
return SobotOkHttpUtils return SobotOkHttpUtils
.post() .post()
.tag(cancelTag) .tag(cancelTag)
.url(url_str) .url(url_str)
.headers(headerMap) .headers(headerMap)
.params(paramMap) .params(paramMap)
.build() .build()
.readTimeOut(8 * 1000) .readTimeOut(8 * 1000)
.writeTimeOut(8 * 1000) .writeTimeOut(8 * 1000)
.connTimeOut(8 * 1000).execute(); .connTimeOut(8 * 1000).execute();
} }
public interface StringCallBack { public interface StringCallBack {
......
...@@ -6,27 +6,23 @@ import com.sobot.network.http.request.RequestCall; ...@@ -6,27 +6,23 @@ import com.sobot.network.http.request.RequestCall;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
public class GetBuilder extends OkHttpRequestBuilder implements HasParamsable import okhttp3.MediaType;
{
public class GetBuilder extends OkHttpRequestBuilder implements HasParamsable {
@Override @Override
public RequestCall build() public RequestCall build() {
{ if (params != null) {
if (params != null)
{
url = appendParams(url, params); url = appendParams(url, params);
} }
return new GetRequest(url, tag, params, headers).build(); return new GetRequest(url, tag, params, headers).build();
} }
protected String appendParams(String url, Map<String, Object> params) protected String appendParams(String url, Map<String, Object> params) {
{
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(url + "?"); sb.append(url + "?");
if (params != null && !params.isEmpty()) if (params != null && !params.isEmpty()) {
{ for (String key : params.keySet()) {
for (String key : params.keySet())
{
sb.append(key).append("=").append(params.get(key)).append("&"); sb.append(key).append("=").append(params.get(key)).append("&");
} }
} }
...@@ -36,52 +32,50 @@ public class GetBuilder extends OkHttpRequestBuilder implements HasParamsable ...@@ -36,52 +32,50 @@ public class GetBuilder extends OkHttpRequestBuilder implements HasParamsable
} }
@Override @Override
public GetBuilder url(String url) public GetBuilder url(String url) {
{
this.url = url; this.url = url;
return this; return this;
} }
@Override @Override
public GetBuilder tag(Object tag) public GetBuilder tag(Object tag) {
{ this.tag = tag;
this.tag = tag ;
return this; return this;
} }
@Override @Override
public GetBuilder params(Map<String, Object> params) public GetBuilder params(Map<String, Object> params) {
{
this.params = params; this.params = params;
return this; return this;
} }
@Override @Override
public GetBuilder addParams(String key, String val) public GetBuilder addParams(String key, String val) {
{ if (this.params == null) {
if (this.params == null) params = new LinkedHashMap<String, Object>();
{
params = new LinkedHashMap<String,Object>();
} }
params.put(key, val); params.put(key, val);
return this; return this;
} }
@Override @Override
public GetBuilder headers(Map<String, String> headers) public GetBuilder headers(Map<String, String> headers) {
{
this.headers = headers; this.headers = headers;
return this; return this;
} }
@Override @Override
public GetBuilder addHeader(String key, String val) public GetBuilder addHeader(String key, String val) {
{ if (this.headers == null) {
if (this.headers == null) headers = new LinkedHashMap<String, String>();
{
headers = new LinkedHashMap<String,String>();
} }
headers.put(key, val); headers.put(key, val);
return this; return this;
} }
@Override
public GetBuilder mediaType(MediaType mediaType) {
this.mediaType=mediaType;
return this;
}
} }
...@@ -9,6 +9,6 @@ public class HeadBuilder extends GetBuilder ...@@ -9,6 +9,6 @@ public class HeadBuilder extends GetBuilder
@Override @Override
public RequestCall build() public RequestCall build()
{ {
return new OtherRequest(null, null, SobotOkHttpUtils.METHOD.HEAD, url, tag, params, headers).build(); return new OtherRequest(null, mediaType,null, SobotOkHttpUtils.METHOD.HEAD, url, tag, params, headers).build();
} }
} }
...@@ -4,12 +4,14 @@ import com.sobot.network.http.request.RequestCall; ...@@ -4,12 +4,14 @@ import com.sobot.network.http.request.RequestCall;
import java.util.Map; import java.util.Map;
public abstract class OkHttpRequestBuilder import okhttp3.MediaType;
{
public abstract class OkHttpRequestBuilder {
protected String url; protected String url;
protected Object tag; protected Object tag;
protected Map<String, String> headers; protected Map<String, String> headers;
protected Map<String, Object> params; protected Map<String, Object> params;
protected MediaType mediaType;
public abstract OkHttpRequestBuilder url(String url); public abstract OkHttpRequestBuilder url(String url);
...@@ -19,6 +21,8 @@ public abstract class OkHttpRequestBuilder ...@@ -19,6 +21,8 @@ public abstract class OkHttpRequestBuilder
public abstract OkHttpRequestBuilder addHeader(String key, String val); public abstract OkHttpRequestBuilder addHeader(String key, String val);
public abstract OkHttpRequestBuilder mediaType(MediaType mediaType);
public abstract RequestCall build(); public abstract RequestCall build();
......
...@@ -6,68 +6,65 @@ import com.sobot.network.http.request.RequestCall; ...@@ -6,68 +6,65 @@ import com.sobot.network.http.request.RequestCall;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import okhttp3.MediaType;
import okhttp3.RequestBody; import okhttp3.RequestBody;
/** /**
* DELETE、PUT、PATCH等其他方法 * DELETE、PUT、PATCH等其他方法
*/ */
public class OtherRequestBuilder extends OkHttpRequestBuilder public class OtherRequestBuilder extends OkHttpRequestBuilder {
{
private RequestBody requestBody; private RequestBody requestBody;
private String method; private String method;
private String content; private String content;
private MediaType mediaType;
public OtherRequestBuilder(String method) public OtherRequestBuilder(String method) {
{
this.method = method; this.method = method;
} }
@Override @Override
public RequestCall build() public RequestCall build() {
{ return new OtherRequest(requestBody, mediaType, content, method, url, tag, params, headers).build();
return new OtherRequest(requestBody, content, method, url, tag, params, headers).build();
} }
public OtherRequestBuilder requestBody(RequestBody requestBody) public OtherRequestBuilder mediaType(MediaType mediaType) {
{ this.mediaType = mediaType;
return this;
}
public OtherRequestBuilder requestBody(RequestBody requestBody) {
this.requestBody = requestBody; this.requestBody = requestBody;
return this; return this;
} }
public OtherRequestBuilder requestBody(String content) public OtherRequestBuilder requestBody(String content) {
{
this.content = content; this.content = content;
return this; return this;
} }
@Override @Override
public OtherRequestBuilder url(String url) public OtherRequestBuilder url(String url) {
{
this.url = url; this.url = url;
return this; return this;
} }
@Override @Override
public OtherRequestBuilder tag(Object tag) public OtherRequestBuilder tag(Object tag) {
{
this.tag = tag; this.tag = tag;
return this; return this;
} }
@Override @Override
public OtherRequestBuilder headers(Map<String, String> headers) public OtherRequestBuilder headers(Map<String, String> headers) {
{
this.headers = headers; this.headers = headers;
return this; return this;
} }
@Override @Override
public OtherRequestBuilder addHeader(String key, String val) public OtherRequestBuilder addHeader(String key, String val) {
{ if (this.headers == null) {
if (this.headers == null) headers = new LinkedHashMap<String, String>();
{
headers = new LinkedHashMap<String,String>();
} }
headers.put(key, val); headers.put(key, val);
return this; return this;
......
...@@ -9,57 +9,48 @@ import java.util.Map; ...@@ -9,57 +9,48 @@ import java.util.Map;
import okhttp3.MediaType; import okhttp3.MediaType;
public class PostFileBuilder extends OkHttpRequestBuilder public class PostFileBuilder extends OkHttpRequestBuilder {
{
private File file; private File file;
private MediaType mediaType; private MediaType mediaType;
public OkHttpRequestBuilder file(File file) public PostFileBuilder file(File file) {
{
this.file = file; this.file = file;
return this; return this;
} }
public OkHttpRequestBuilder mediaType(MediaType mediaType) public PostFileBuilder mediaType(MediaType mediaType) {
{
this.mediaType = mediaType; this.mediaType = mediaType;
return this; return this;
} }
@Override @Override
public RequestCall build() public RequestCall build() {
{
return new PostFileRequest(url, tag, params, headers, file, mediaType).build(); return new PostFileRequest(url, tag, params, headers, file, mediaType).build();
} }
@Override @Override
public PostFileBuilder url(String url) public PostFileBuilder url(String url) {
{
this.url = url; this.url = url;
return this; return this;
} }
@Override @Override
public PostFileBuilder tag(Object tag) public PostFileBuilder tag(Object tag) {
{
this.tag = tag; this.tag = tag;
return this; return this;
} }
@Override @Override
public PostFileBuilder headers(Map<String, String> headers) public PostFileBuilder headers(Map<String, String> headers) {
{
this.headers = headers; this.headers = headers;
return this; return this;
} }
@Override @Override
public PostFileBuilder addHeader(String key, String val) public PostFileBuilder addHeader(String key, String val) {
{ if (this.headers == null) {
if (this.headers == null) headers = new LinkedHashMap<String, String>();
{
headers = new LinkedHashMap<String,String>();
} }
headers.put(key, val); headers.put(key, val);
return this; return this;
......
...@@ -10,40 +10,36 @@ import java.util.LinkedHashMap; ...@@ -10,40 +10,36 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class PostFormBuilder extends OkHttpRequestBuilder implements HasParamsable import okhttp3.MediaType;
{
public class PostFormBuilder extends OkHttpRequestBuilder implements HasParamsable {
private List<FileInput> files = new ArrayList<FileInput>(); private List<FileInput> files = new ArrayList<FileInput>();
private ProgressRequestBody.UploadInterceptor uploadInterceptor; private ProgressRequestBody.UploadInterceptor uploadInterceptor;
@Override @Override
public RequestCall build() public RequestCall build() {
{
return new PostFormRequest(url, tag, params, headers, files).uploadInterceptor(uploadInterceptor).build(); return new PostFormRequest(url, tag, params, headers, files).uploadInterceptor(uploadInterceptor).build();
} }
public PostFormBuilder addFile(String name, String filename, File file) public PostFormBuilder addFile(String name, String filename, File file) {
{
files.add(new FileInput(name, filename, file)); files.add(new FileInput(name, filename, file));
return this; return this;
} }
public static class FileInput public static class FileInput {
{
public String key; public String key;
public String filename; public String filename;
public File file; public File file;
public FileInput(String name, String filename, File file) public FileInput(String name, String filename, File file) {
{
this.key = name; this.key = name;
this.filename = filename; this.filename = filename;
this.file = file; this.file = file;
} }
@Override @Override
public String toString() public String toString() {
{
return "FileInput{" + return "FileInput{" +
"key='" + key + '\'' + "key='" + key + '\'' +
", filename='" + filename + '\'' + ", filename='" + filename + '\'' +
...@@ -54,56 +50,54 @@ public class PostFormBuilder extends OkHttpRequestBuilder implements HasParamsab ...@@ -54,56 +50,54 @@ public class PostFormBuilder extends OkHttpRequestBuilder implements HasParamsab
// //
@Override @Override
public PostFormBuilder url(String url) public PostFormBuilder url(String url) {
{
this.url = url; this.url = url;
return this; return this;
} }
@Override @Override
public PostFormBuilder tag(Object tag) public PostFormBuilder tag(Object tag) {
{
this.tag = tag; this.tag = tag;
return this; return this;
} }
@Override @Override
public PostFormBuilder params(Map<String, Object> params) public PostFormBuilder params(Map<String, Object> params) {
{
this.params = params; this.params = params;
return this; return this;
} }
@Override @Override
public PostFormBuilder addParams(String key, String val) public PostFormBuilder addParams(String key, String val) {
{ if (this.params == null) {
if (this.params == null) params = new LinkedHashMap<String, Object>();
{
params = new LinkedHashMap<String,Object>();
} }
params.put(key, val); params.put(key, val);
return this; return this;
} }
@Override @Override
public PostFormBuilder headers(Map<String, String> headers) public PostFormBuilder headers(Map<String, String> headers) {
{
this.headers = headers; this.headers = headers;
return this; return this;
} }
@Override @Override
public PostFormBuilder addHeader(String key, String val) public PostFormBuilder addHeader(String key, String val) {
{ if (this.headers == null) {
if (this.headers == null) headers = new LinkedHashMap<String, String>();
{
headers = new LinkedHashMap<String,String>();
} }
headers.put(key, val); headers.put(key, val);
return this; return this;
} }
@Override
public PostFormBuilder mediaType(MediaType mediaType) {
this.mediaType = mediaType;
return this;
}
public PostFormBuilder uploadInterceptor(ProgressRequestBody.UploadInterceptor uploadInterceptor) { public PostFormBuilder uploadInterceptor(ProgressRequestBody.UploadInterceptor uploadInterceptor) {
this.uploadInterceptor = uploadInterceptor; this.uploadInterceptor = uploadInterceptor;
return this; return this;
......
...@@ -10,40 +10,36 @@ import java.util.LinkedHashMap; ...@@ -10,40 +10,36 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class PostMultipartFormBuilder extends OkHttpRequestBuilder implements HasParamsable import okhttp3.MediaType;
{
public class PostMultipartFormBuilder extends OkHttpRequestBuilder implements HasParamsable {
private List<FileInput> files = new ArrayList<FileInput>(); private List<FileInput> files = new ArrayList<FileInput>();
private ProgressRequestBody.UploadInterceptor uploadInterceptor; private ProgressRequestBody.UploadInterceptor uploadInterceptor;
@Override @Override
public RequestCall build() public RequestCall build() {
{
return new PostMultipartFormRequest(url, tag, params, headers, files).uploadInterceptor(uploadInterceptor).build(); return new PostMultipartFormRequest(url, tag, params, headers, files).uploadInterceptor(uploadInterceptor).build();
} }
public PostMultipartFormBuilder addFile(String name, String filename, File file) public PostMultipartFormBuilder addFile(String name, String filename, File file) {
{
files.add(new FileInput(name, filename, file)); files.add(new FileInput(name, filename, file));
return this; return this;
} }
public static class FileInput public static class FileInput {
{
public String key; public String key;
public String filename; public String filename;
public File file; public File file;
public FileInput(String name, String filename, File file) public FileInput(String name, String filename, File file) {
{
this.key = name; this.key = name;
this.filename = filename; this.filename = filename;
this.file = file; this.file = file;
} }
@Override @Override
public String toString() public String toString() {
{
return "FileInput{" + return "FileInput{" +
"key='" + key + '\'' + "key='" + key + '\'' +
", filename='" + filename + '\'' + ", filename='" + filename + '\'' +
...@@ -54,51 +50,43 @@ public class PostMultipartFormBuilder extends OkHttpRequestBuilder implements Ha ...@@ -54,51 +50,43 @@ public class PostMultipartFormBuilder extends OkHttpRequestBuilder implements Ha
// //
@Override @Override
public PostMultipartFormBuilder url(String url) public PostMultipartFormBuilder url(String url) {
{
this.url = url; this.url = url;
return this; return this;
} }
@Override @Override
public PostMultipartFormBuilder tag(Object tag) public PostMultipartFormBuilder tag(Object tag) {
{
this.tag = tag; this.tag = tag;
return this; return this;
} }
@Override @Override
public PostMultipartFormBuilder params(Map<String, Object> params) public PostMultipartFormBuilder params(Map<String, Object> params) {
{
this.params = params; this.params = params;
return this; return this;
} }
@Override @Override
public PostMultipartFormBuilder addParams(String key, String val) public PostMultipartFormBuilder addParams(String key, String val) {
{ if (this.params == null) {
if (this.params == null) params = new LinkedHashMap<String, Object>();
{
params = new LinkedHashMap<String,Object>();
} }
params.put(key, val); params.put(key, val);
return this; return this;
} }
@Override @Override
public PostMultipartFormBuilder headers(Map<String, String> headers) public PostMultipartFormBuilder headers(Map<String, String> headers) {
{
this.headers = headers; this.headers = headers;
return this; return this;
} }
@Override @Override
public PostMultipartFormBuilder addHeader(String key, String val) public PostMultipartFormBuilder addHeader(String key, String val) {
{ if (this.headers == null) {
if (this.headers == null) headers = new LinkedHashMap<String, String>();
{
headers = new LinkedHashMap<String,String>();
} }
headers.put(key, val); headers.put(key, val);
return this; return this;
...@@ -109,5 +97,9 @@ public class PostMultipartFormBuilder extends OkHttpRequestBuilder implements Ha ...@@ -109,5 +97,9 @@ public class PostMultipartFormBuilder extends OkHttpRequestBuilder implements Ha
return this; return this;
} }
public PostMultipartFormBuilder mediaType(MediaType mediaType) {
this.mediaType = mediaType;
return this;
}
} }
...@@ -8,58 +8,49 @@ import java.util.Map; ...@@ -8,58 +8,49 @@ import java.util.Map;
import okhttp3.MediaType; import okhttp3.MediaType;
public class PostStringBuilder extends OkHttpRequestBuilder public class PostStringBuilder extends OkHttpRequestBuilder {
{
private String content; private String content;
private MediaType mediaType; private MediaType mediaType;
public PostStringBuilder content(String content) public PostStringBuilder content(String content) {
{
this.content = content; this.content = content;
return this; return this;
} }
public PostStringBuilder mediaType(MediaType mediaType) public PostStringBuilder mediaType(MediaType mediaType) {
{
this.mediaType = mediaType; this.mediaType = mediaType;
return this; return this;
} }
@Override @Override
public RequestCall build() public RequestCall build() {
{
return new PostStringRequest(url, tag, params, headers, content, mediaType).build(); return new PostStringRequest(url, tag, params, headers, content, mediaType).build();
} }
@Override @Override
public PostStringBuilder url(String url) public PostStringBuilder url(String url) {
{
this.url = url; this.url = url;
return this; return this;
} }
@Override @Override
public PostStringBuilder tag(Object tag) public PostStringBuilder tag(Object tag) {
{
this.tag = tag; this.tag = tag;
return this; return this;
} }
@Override @Override
public PostStringBuilder headers(Map<String, String> headers) public PostStringBuilder headers(Map<String, String> headers) {
{
this.headers = headers; this.headers = headers;
return this; return this;
} }
@Override @Override
public PostStringBuilder addHeader(String key, String val) public PostStringBuilder addHeader(String key, String val) {
{ if (this.headers == null) {
if (this.headers == null) headers = new LinkedHashMap<String, String>();
{
headers = new LinkedHashMap<String,String>();
} }
headers.put(key, val); headers.put(key, val);
return this; return this;
......
...@@ -12,56 +12,50 @@ import okhttp3.Request; ...@@ -12,56 +12,50 @@ import okhttp3.Request;
import okhttp3.RequestBody; import okhttp3.RequestBody;
import okhttp3.internal.http.HttpMethod; import okhttp3.internal.http.HttpMethod;
public class OtherRequest extends OkHttpRequest public class OtherRequest extends OkHttpRequest {
{ private static MediaType MEDIA_TYPE_PLAIN = MediaType.parse("application/json;charset=utf-8");
private static MediaType MEDIA_TYPE_PLAIN = MediaType.parse("text/plain;charset=utf-8");
private RequestBody requestBody; private RequestBody requestBody;
private String method; private String method;
private String content; private String content;
private MediaType mediaType;
public OtherRequest(RequestBody requestBody, String content, String method, String url, Object tag, Map<String, Object> params, Map<String, String> headers) public OtherRequest(RequestBody requestBody, MediaType mediaType, String content, String method, String url, Object tag, Map<String, Object> params, Map<String, String> headers) {
{
super(url, tag, params, headers); super(url, tag, params, headers);
this.requestBody = requestBody; this.requestBody = requestBody;
this.method = method; this.method = method;
this.content = content; this.content = content;
this.mediaType = mediaType;
if (this.mediaType == null) {
this.mediaType = MEDIA_TYPE_PLAIN;
}
} }
@Override @Override
protected RequestBody buildRequestBody() protected RequestBody buildRequestBody() {
{ if (requestBody == null && TextUtils.isEmpty(content) && HttpMethod.requiresRequestBody(method)) {
if (requestBody == null && TextUtils.isEmpty(content) && HttpMethod.requiresRequestBody(method))
{
Exceptions.illegalArgument("requestBody and content can not be null in method:" + method); Exceptions.illegalArgument("requestBody and content can not be null in method:" + method);
} }
if (requestBody == null && !TextUtils.isEmpty(content)) if (requestBody == null && !TextUtils.isEmpty(content)) {
{ requestBody = RequestBody.create(mediaType, content);
requestBody = RequestBody.create(MEDIA_TYPE_PLAIN, content);
} }
return requestBody; return requestBody;
} }
@Override @Override
protected Request buildRequest(RequestBody requestBody) protected Request buildRequest(RequestBody requestBody) {
{ if (method.equals(SobotOkHttpUtils.METHOD.PUT)) {
if (method.equals(SobotOkHttpUtils.METHOD.PUT))
{
builder.put(requestBody); builder.put(requestBody);
} else if (method.equals(SobotOkHttpUtils.METHOD.DELETE)) } else if (method.equals(SobotOkHttpUtils.METHOD.DELETE)) {
{
if (requestBody == null) if (requestBody == null)
builder.delete(); builder.delete();
else else
builder.delete(requestBody); builder.delete(requestBody);
} else if (method.equals(SobotOkHttpUtils.METHOD.HEAD)) } else if (method.equals(SobotOkHttpUtils.METHOD.HEAD)) {
{
builder.head(); builder.head();
} else if (method.equals(SobotOkHttpUtils.METHOD.PATCH)) } else if (method.equals(SobotOkHttpUtils.METHOD.PATCH)) {
{
builder.patch(requestBody); builder.patch(requestBody);
} }
......
...@@ -10,7 +10,7 @@ import okhttp3.RequestBody; ...@@ -10,7 +10,7 @@ import okhttp3.RequestBody;
public class PostStringRequest extends OkHttpRequest public class PostStringRequest extends OkHttpRequest
{ {
private static MediaType MEDIA_TYPE_PLAIN = MediaType.parse("text/plain;charset=utf-8"); private static MediaType MEDIA_TYPE_PLAIN = MediaType.parse("application/json;charset=utf-8");
private String content; private String content;
private MediaType mediaType; private MediaType mediaType;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment