Commit 77949842 by 郑娜伟

net 发布1.1.5 支持put

common 发布1.2.3
parent 0f4802cd
......@@ -21,12 +21,12 @@ dependencies {
// api 'com.sobot.library:utils:1.1.1'
// 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:utils:1.1.1'
// 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'
}
......
......@@ -13,7 +13,7 @@ ext {
PUBLISH_GROUP_ID = "com.sobot.library" //项目包名
PUBLISH_ARTIFACT_ID = 'sobotcommon' //项目名
// PUBLISH_ARTIFACT_ID = 'sobotcommon_x' //项目名
PUBLISH_VERSION = '1.2.2' //版本号
PUBLISH_VERSION = '1.2.3' //版本号
}
......
......@@ -12,7 +12,7 @@ task androidSourcesJar(type: Jar) {
ext {
PUBLISH_GROUP_ID = "com.sobot.library" //项目包名
PUBLISH_ARTIFACT_ID = 'net' //项目名
PUBLISH_VERSION = '1.1.3' //版本号
PUBLISH_VERSION = '1.1.5' //版本号
}
......
......@@ -23,6 +23,7 @@ import java.util.Map;
import java.util.Random;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.Response;
......@@ -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,
final FileCallBack callback) {
SobotNetLogUtils.i("下载地址:" + url_str);
......@@ -444,16 +494,16 @@ public class HttpBaseUtils {
if (headerMap == null) {
headerMap = new HashMap<>();
}
return SobotOkHttpUtils
.post()
.tag(cancelTag)
.url(url_str)
.headers(headerMap)
.params(paramMap)
.build()
.readTimeOut(8 * 1000)
.writeTimeOut(8 * 1000)
.connTimeOut(8 * 1000).execute();
return SobotOkHttpUtils
.post()
.tag(cancelTag)
.url(url_str)
.headers(headerMap)
.params(paramMap)
.build()
.readTimeOut(8 * 1000)
.writeTimeOut(8 * 1000)
.connTimeOut(8 * 1000).execute();
}
public interface StringCallBack {
......
......@@ -6,27 +6,23 @@ import com.sobot.network.http.request.RequestCall;
import java.util.LinkedHashMap;
import java.util.Map;
public class GetBuilder extends OkHttpRequestBuilder implements HasParamsable
{
import okhttp3.MediaType;
public class GetBuilder extends OkHttpRequestBuilder implements HasParamsable {
@Override
public RequestCall build()
{
if (params != null)
{
public RequestCall build() {
if (params != null) {
url = appendParams(url, params);
}
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();
sb.append(url + "?");
if (params != null && !params.isEmpty())
{
for (String key : params.keySet())
{
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
sb.append(key).append("=").append(params.get(key)).append("&");
}
}
......@@ -36,52 +32,50 @@ public class GetBuilder extends OkHttpRequestBuilder implements HasParamsable
}
@Override
public GetBuilder url(String url)
{
public GetBuilder url(String url) {
this.url = url;
return this;
}
@Override
public GetBuilder tag(Object tag)
{
this.tag = tag ;
public GetBuilder tag(Object tag) {
this.tag = tag;
return this;
}
@Override
public GetBuilder params(Map<String, Object> params)
{
public GetBuilder params(Map<String, Object> params) {
this.params = params;
return this;
}
@Override
public GetBuilder addParams(String key, String val)
{
if (this.params == null)
{
params = new LinkedHashMap<String,Object>();
public GetBuilder addParams(String key, String val) {
if (this.params == null) {
params = new LinkedHashMap<String, Object>();
}
params.put(key, val);
return this;
}
@Override
public GetBuilder headers(Map<String, String> headers)
{
public GetBuilder headers(Map<String, String> headers) {
this.headers = headers;
return this;
}
@Override
public GetBuilder addHeader(String key, String val)
{
if (this.headers == null)
{
headers = new LinkedHashMap<String,String>();
public GetBuilder addHeader(String key, String val) {
if (this.headers == null) {
headers = new LinkedHashMap<String, String>();
}
headers.put(key, val);
return this;
}
@Override
public GetBuilder mediaType(MediaType mediaType) {
this.mediaType=mediaType;
return this;
}
}
......@@ -9,6 +9,6 @@ public class HeadBuilder extends GetBuilder
@Override
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;
import java.util.Map;
public abstract class OkHttpRequestBuilder
{
import okhttp3.MediaType;
public abstract class OkHttpRequestBuilder {
protected String url;
protected Object tag;
protected Map<String, String> headers;
protected Map<String, Object> params;
protected MediaType mediaType;
public abstract OkHttpRequestBuilder url(String url);
......@@ -19,6 +21,8 @@ public abstract class OkHttpRequestBuilder
public abstract OkHttpRequestBuilder addHeader(String key, String val);
public abstract OkHttpRequestBuilder mediaType(MediaType mediaType);
public abstract RequestCall build();
......
......@@ -6,68 +6,65 @@ import com.sobot.network.http.request.RequestCall;
import java.util.LinkedHashMap;
import java.util.Map;
import okhttp3.MediaType;
import okhttp3.RequestBody;
/**
* DELETE、PUT、PATCH等其他方法
*/
public class OtherRequestBuilder extends OkHttpRequestBuilder
{
public class OtherRequestBuilder extends OkHttpRequestBuilder {
private RequestBody requestBody;
private String method;
private String content;
private MediaType mediaType;
public OtherRequestBuilder(String method)
{
public OtherRequestBuilder(String method) {
this.method = method;
}
@Override
public RequestCall build()
{
return new OtherRequest(requestBody, content, method, url, tag, params, headers).build();
public RequestCall build() {
return new OtherRequest(requestBody, mediaType, 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;
return this;
}
public OtherRequestBuilder requestBody(String content)
{
public OtherRequestBuilder requestBody(String content) {
this.content = content;
return this;
}
@Override
public OtherRequestBuilder url(String url)
{
public OtherRequestBuilder url(String url) {
this.url = url;
return this;
}
@Override
public OtherRequestBuilder tag(Object tag)
{
public OtherRequestBuilder tag(Object tag) {
this.tag = tag;
return this;
}
@Override
public OtherRequestBuilder headers(Map<String, String> headers)
{
public OtherRequestBuilder headers(Map<String, String> headers) {
this.headers = headers;
return this;
}
@Override
public OtherRequestBuilder addHeader(String key, String val)
{
if (this.headers == null)
{
headers = new LinkedHashMap<String,String>();
public OtherRequestBuilder addHeader(String key, String val) {
if (this.headers == null) {
headers = new LinkedHashMap<String, String>();
}
headers.put(key, val);
return this;
......
......@@ -9,57 +9,48 @@ import java.util.Map;
import okhttp3.MediaType;
public class PostFileBuilder extends OkHttpRequestBuilder
{
public class PostFileBuilder extends OkHttpRequestBuilder {
private File file;
private MediaType mediaType;
public OkHttpRequestBuilder file(File file)
{
public PostFileBuilder file(File file) {
this.file = file;
return this;
}
public OkHttpRequestBuilder mediaType(MediaType mediaType)
{
public PostFileBuilder mediaType(MediaType mediaType) {
this.mediaType = mediaType;
return this;
}
@Override
public RequestCall build()
{
public RequestCall build() {
return new PostFileRequest(url, tag, params, headers, file, mediaType).build();
}
@Override
public PostFileBuilder url(String url)
{
public PostFileBuilder url(String url) {
this.url = url;
return this;
}
@Override
public PostFileBuilder tag(Object tag)
{
public PostFileBuilder tag(Object tag) {
this.tag = tag;
return this;
}
@Override
public PostFileBuilder headers(Map<String, String> headers)
{
public PostFileBuilder headers(Map<String, String> headers) {
this.headers = headers;
return this;
}
@Override
public PostFileBuilder addHeader(String key, String val)
{
if (this.headers == null)
{
headers = new LinkedHashMap<String,String>();
public PostFileBuilder addHeader(String key, String val) {
if (this.headers == null) {
headers = new LinkedHashMap<String, String>();
}
headers.put(key, val);
return this;
......
......@@ -10,40 +10,36 @@ import java.util.LinkedHashMap;
import java.util.List;
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 ProgressRequestBody.UploadInterceptor uploadInterceptor;
@Override
public RequestCall build()
{
public RequestCall 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));
return this;
}
public static class FileInput
{
public static class FileInput {
public String key;
public String filename;
public File file;
public FileInput(String name, String filename, File file)
{
public FileInput(String name, String filename, File file) {
this.key = name;
this.filename = filename;
this.file = file;
}
@Override
public String toString()
{
public String toString() {
return "FileInput{" +
"key='" + key + '\'' +
", filename='" + filename + '\'' +
......@@ -54,56 +50,54 @@ public class PostFormBuilder extends OkHttpRequestBuilder implements HasParamsab
//
@Override
public PostFormBuilder url(String url)
{
public PostFormBuilder url(String url) {
this.url = url;
return this;
}
@Override
public PostFormBuilder tag(Object tag)
{
public PostFormBuilder tag(Object tag) {
this.tag = tag;
return this;
}
@Override
public PostFormBuilder params(Map<String, Object> params)
{
public PostFormBuilder params(Map<String, Object> params) {
this.params = params;
return this;
}
@Override
public PostFormBuilder addParams(String key, String val)
{
if (this.params == null)
{
params = new LinkedHashMap<String,Object>();
public PostFormBuilder addParams(String key, String val) {
if (this.params == null) {
params = new LinkedHashMap<String, Object>();
}
params.put(key, val);
return this;
}
@Override
public PostFormBuilder headers(Map<String, String> headers)
{
public PostFormBuilder headers(Map<String, String> headers) {
this.headers = headers;
return this;
}
@Override
public PostFormBuilder addHeader(String key, String val)
{
if (this.headers == null)
{
headers = new LinkedHashMap<String,String>();
public PostFormBuilder addHeader(String key, String val) {
if (this.headers == null) {
headers = new LinkedHashMap<String, String>();
}
headers.put(key, val);
return this;
}
@Override
public PostFormBuilder mediaType(MediaType mediaType) {
this.mediaType = mediaType;
return this;
}
public PostFormBuilder uploadInterceptor(ProgressRequestBody.UploadInterceptor uploadInterceptor) {
this.uploadInterceptor = uploadInterceptor;
return this;
......
......@@ -10,40 +10,36 @@ import java.util.LinkedHashMap;
import java.util.List;
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 ProgressRequestBody.UploadInterceptor uploadInterceptor;
@Override
public RequestCall build()
{
public RequestCall 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));
return this;
}
public static class FileInput
{
public static class FileInput {
public String key;
public String filename;
public File file;
public FileInput(String name, String filename, File file)
{
public FileInput(String name, String filename, File file) {
this.key = name;
this.filename = filename;
this.file = file;
}
@Override
public String toString()
{
public String toString() {
return "FileInput{" +
"key='" + key + '\'' +
", filename='" + filename + '\'' +
......@@ -54,51 +50,43 @@ public class PostMultipartFormBuilder extends OkHttpRequestBuilder implements Ha
//
@Override
public PostMultipartFormBuilder url(String url)
{
public PostMultipartFormBuilder url(String url) {
this.url = url;
return this;
}
@Override
public PostMultipartFormBuilder tag(Object tag)
{
public PostMultipartFormBuilder tag(Object tag) {
this.tag = tag;
return this;
}
@Override
public PostMultipartFormBuilder params(Map<String, Object> params)
{
public PostMultipartFormBuilder params(Map<String, Object> params) {
this.params = params;
return this;
}
@Override
public PostMultipartFormBuilder addParams(String key, String val)
{
if (this.params == null)
{
params = new LinkedHashMap<String,Object>();
public PostMultipartFormBuilder addParams(String key, String val) {
if (this.params == null) {
params = new LinkedHashMap<String, Object>();
}
params.put(key, val);
return this;
}
@Override
public PostMultipartFormBuilder headers(Map<String, String> headers)
{
public PostMultipartFormBuilder headers(Map<String, String> headers) {
this.headers = headers;
return this;
}
@Override
public PostMultipartFormBuilder addHeader(String key, String val)
{
if (this.headers == null)
{
headers = new LinkedHashMap<String,String>();
public PostMultipartFormBuilder addHeader(String key, String val) {
if (this.headers == null) {
headers = new LinkedHashMap<String, String>();
}
headers.put(key, val);
return this;
......@@ -109,5 +97,9 @@ public class PostMultipartFormBuilder extends OkHttpRequestBuilder implements Ha
return this;
}
public PostMultipartFormBuilder mediaType(MediaType mediaType) {
this.mediaType = mediaType;
return this;
}
}
......@@ -8,58 +8,49 @@ import java.util.Map;
import okhttp3.MediaType;
public class PostStringBuilder extends OkHttpRequestBuilder
{
public class PostStringBuilder extends OkHttpRequestBuilder {
private String content;
private MediaType mediaType;
public PostStringBuilder content(String content)
{
public PostStringBuilder content(String content) {
this.content = content;
return this;
}
public PostStringBuilder mediaType(MediaType mediaType)
{
public PostStringBuilder mediaType(MediaType mediaType) {
this.mediaType = mediaType;
return this;
}
@Override
public RequestCall build()
{
public RequestCall build() {
return new PostStringRequest(url, tag, params, headers, content, mediaType).build();
}
@Override
public PostStringBuilder url(String url)
{
public PostStringBuilder url(String url) {
this.url = url;
return this;
}
@Override
public PostStringBuilder tag(Object tag)
{
public PostStringBuilder tag(Object tag) {
this.tag = tag;
return this;
}
@Override
public PostStringBuilder headers(Map<String, String> headers)
{
public PostStringBuilder headers(Map<String, String> headers) {
this.headers = headers;
return this;
}
@Override
public PostStringBuilder addHeader(String key, String val)
{
if (this.headers == null)
{
headers = new LinkedHashMap<String,String>();
public PostStringBuilder addHeader(String key, String val) {
if (this.headers == null) {
headers = new LinkedHashMap<String, String>();
}
headers.put(key, val);
return this;
......
......@@ -12,56 +12,50 @@ import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.internal.http.HttpMethod;
public class OtherRequest extends OkHttpRequest
{
private static MediaType MEDIA_TYPE_PLAIN = MediaType.parse("text/plain;charset=utf-8");
public class OtherRequest extends OkHttpRequest {
private static MediaType MEDIA_TYPE_PLAIN = MediaType.parse("application/json;charset=utf-8");
private RequestBody requestBody;
private String method;
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);
this.requestBody = requestBody;
this.method = method;
this.content = content;
this.mediaType = mediaType;
if (this.mediaType == null) {
this.mediaType = MEDIA_TYPE_PLAIN;
}
}
@Override
protected RequestBody buildRequestBody()
{
if (requestBody == null && TextUtils.isEmpty(content) && HttpMethod.requiresRequestBody(method))
{
protected RequestBody buildRequestBody() {
if (requestBody == null && TextUtils.isEmpty(content) && HttpMethod.requiresRequestBody(method)) {
Exceptions.illegalArgument("requestBody and content can not be null in method:" + method);
}
if (requestBody == null && !TextUtils.isEmpty(content))
{
requestBody = RequestBody.create(MEDIA_TYPE_PLAIN, content);
if (requestBody == null && !TextUtils.isEmpty(content)) {
requestBody = RequestBody.create(mediaType, content);
}
return requestBody;
}
@Override
protected Request buildRequest(RequestBody requestBody)
{
if (method.equals(SobotOkHttpUtils.METHOD.PUT))
{
protected Request buildRequest(RequestBody requestBody) {
if (method.equals(SobotOkHttpUtils.METHOD.PUT)) {
builder.put(requestBody);
} else if (method.equals(SobotOkHttpUtils.METHOD.DELETE))
{
} else if (method.equals(SobotOkHttpUtils.METHOD.DELETE)) {
if (requestBody == null)
builder.delete();
else
builder.delete(requestBody);
} else if (method.equals(SobotOkHttpUtils.METHOD.HEAD))
{
} else if (method.equals(SobotOkHttpUtils.METHOD.HEAD)) {
builder.head();
} else if (method.equals(SobotOkHttpUtils.METHOD.PATCH))
{
} else if (method.equals(SobotOkHttpUtils.METHOD.PATCH)) {
builder.patch(requestBody);
}
......
......@@ -10,7 +10,7 @@ import okhttp3.RequestBody;
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 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