Commit 440b8dd5 by app_dev@sobot.com

sobot net 1.0.3

parent 29c28144
...@@ -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.0.1' //版本号 PUBLISH_VERSION = '1.0.3' //版本号
} }
......
...@@ -9,15 +9,16 @@ import com.sobot.network.http.log.SobotNetLogUtils; ...@@ -9,15 +9,16 @@ import com.sobot.network.http.log.SobotNetLogUtils;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
import okhttp3.MediaType;
import okhttp3.Response; import okhttp3.Response;
public class SobotHttpUtils { public class SobotHttpUtils {
// ##################### 网络请求 ######################### // ##################### 网络请求 #########################
public static void doPost(Object cancelTag, String url, Map<String, String> map, public static void doPost(Object cancelTag, String url, Map<String, String> paramsMap,
HttpUtils.StringCallBack call) { HttpUtils.StringCallBack call) {
HttpUtils.getInstance().doPost(cancelTag, url, map, null, call); HttpUtils.getInstance().doPost(cancelTag, url, paramsMap, null, call);
} }
public static void doPost(Object cancelTag, String url, Map<String, String> paramsMap, Map<String, String> headerMap, public static void doPost(Object cancelTag, String url, Map<String, String> paramsMap, Map<String, String> headerMap,
...@@ -25,14 +26,29 @@ public class SobotHttpUtils { ...@@ -25,14 +26,29 @@ public class SobotHttpUtils {
HttpUtils.getInstance().doPost(cancelTag, url, paramsMap, headerMap, call); HttpUtils.getInstance().doPost(cancelTag, url, paramsMap, headerMap, call);
} }
public static void doGet(Object cancelTag, String url, Map<String, String> map, public static void doPostByString(Object cancelTag, String url, Map<String, String> paramsMap, Map<String, String> headerMap, MediaType mediaType,
HttpUtils.StringCallBack call) { HttpUtils.StringCallBack call) {
HttpUtils.getInstance().doGet(cancelTag, url, map, call); HttpUtils.getInstance().doPostByString(cancelTag, url, paramsMap, headerMap, mediaType, call);
}
public static void doPostByJsonString(Object cancelTag, String url, Map<String, String> paramsMap, Map<String, String> headerMap,
HttpUtils.StringCallBack call) {
HttpUtils.getInstance().doPostByJsonString(cancelTag, url, paramsMap, headerMap, call);
}
public static void doGet(Object cancelTag, String url, Map<String, String> paramsMap,
HttpUtils.StringCallBack call) {
HttpUtils.getInstance().doGet(cancelTag, url, paramsMap, null, call);
}
public static void doGet(Object cancelTag, String url, Map<String, String> paramsMap, Map<String, String> headerMap,
HttpUtils.StringCallBack call) {
HttpUtils.getInstance().doGet(cancelTag, url, paramsMap, headerMap, call);
} }
//同步请求 //同步请求
public static Response doPostSync(Object cancelTag, String url, Map<String, String> map) throws IOException { public static Response doPostSync(Object cancelTag, String url, Map<String, String> paramsMap, Map<String, String> headerMap) throws IOException {
return HttpUtils.getInstance().doPostSync(cancelTag, url, map); return HttpUtils.getInstance().doPostSync(cancelTag, url, paramsMap, headerMap);
} }
...@@ -40,24 +56,24 @@ public class SobotHttpUtils { ...@@ -40,24 +56,24 @@ public class SobotHttpUtils {
* 发送文件请求 * 发送文件请求
* *
* @param url * @param url
* @param map * @param paramsMap
* @param filePath * @param filePath
* @param callBack * @param callBack
*/ */
public static void uploadFile(Object cancelTag, String url, Map<String, String> map, public static void uploadFile(Object cancelTag, String url, Map<String, String> paramsMap, Map<String, String> headerMap,
final String filePath, HttpUtils.StringCallBack callBack) { final String filePath, HttpUtils.StringCallBack callBack) {
/////////////////////////////////////////////////////// ///////////////////////////////////////////////////////
if (map != null) { if (paramsMap != null) {
// 基本传递参数 // 基本传递参数
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("sobot---请求参数: url = " + url + ", filePath=" + filePath + " "); sb.append("sobot---请求参数: url = " + url + ", filePath=" + filePath + " ");
for (String key : map.keySet()) { for (String key : paramsMap.keySet()) {
sb.append(key + "=" + map.get(key) + ", "); sb.append(key + "=" + paramsMap.get(key) + ", ");
} }
SobotNetLogUtils.i(sb.toString().substring(0, sb.toString().length() - 2)); SobotNetLogUtils.i(sb.toString().substring(0, sb.toString().length() - 2));
} }
HttpUtils.getInstance().uploadFile(cancelTag, url, map, filePath, callBack); HttpUtils.getInstance().uploadFile(cancelTag, url, paramsMap, headerMap, filePath, callBack);
} }
......
...@@ -11,12 +11,16 @@ import com.sobot.network.http.request.RequestCall; ...@@ -11,12 +11,16 @@ import com.sobot.network.http.request.RequestCall;
import com.sobot.network.http.upload.SobotUpload; import com.sobot.network.http.upload.SobotUpload;
import com.sobot.network.http.upload.SobotUploadTask; import com.sobot.network.http.upload.SobotUploadTask;
import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import okhttp3.Call; import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.Response; import okhttp3.Response;
public class HttpUtils { public class HttpUtils {
...@@ -32,18 +36,19 @@ public class HttpUtils { ...@@ -32,18 +36,19 @@ public class HttpUtils {
return client; return client;
} }
public void doPost(Object cancelTag, final String url_str, final Map<String, String> paramMap, public void doPost(Object cancelTag, final String url_str, Map<String, String> paramMap,
final Map<String, String> headerMap, Map<String, String> headerMap,
final StringCallBack callback) { final StringCallBack callback) {
SobotNetLogUtils.i("请求URL: --> " + url_str); SobotNetLogUtils.i("请求URL: --> " + url_str);
SobotNetLogUtils.i("请求参数: --> " + paramMap); SobotNetLogUtils.i("请求参数: --> " + paramMap);
if (headerMap!=null&&headerMap.size()>0){ if (headerMap == null) {
headerMap = new HashMap<>();
} }
SobotOkHttpUtils SobotOkHttpUtils
.post() .post()
.tag(cancelTag) .tag(cancelTag)
.url(url_str) .url(url_str)
.headers(headerMap)
.params(paramMap) .params(paramMap)
.build() .build()
.readTimeOut(8 * 1000) .readTimeOut(8 * 1000)
...@@ -66,15 +71,63 @@ public class HttpUtils { ...@@ -66,15 +71,63 @@ public class HttpUtils {
}); });
} }
public void doGet(Object cancelTag, final String url_str, final Map<String, String> map, public void doPostByString(Object cancelTag, final String url_str, Map<String, String> paramMap,
Map<String, String> headerMap, MediaType mediaType,
final StringCallBack callback) { final StringCallBack callback) {
SobotNetLogUtils.i("请求URL: --> " + url_str); SobotNetLogUtils.i("请求URL: --> " + url_str);
SobotNetLogUtils.i("请求参数: --> " + map); SobotNetLogUtils.i("请求参数: --> " + paramMap);
if (headerMap == null) {
headerMap = new HashMap<>();
}
SobotOkHttpUtils
.postString()
.tag(cancelTag)
.url(url_str)
.headers(headerMap)
.mediaType(mediaType)
.content(map2Json(paramMap))
.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);
}
});
}
//请求参数为json
public void doPostByJsonString(Object cancelTag, String url_str, Map<String, String> paramMap,
Map<String, String> headerMap,
final StringCallBack callback) {
doPostByString(cancelTag, url_str, paramMap, headerMap, MediaType.get("application/json"), callback);
}
public void doGet(Object cancelTag, final String url_str, final 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<>();
}
SobotOkHttpUtils SobotOkHttpUtils
.get() .get()
.tag(cancelTag) .tag(cancelTag)
.url(url_str) .url(url_str)
.params(map) .params(paramMap)
.headers(headerMap)
.build() .build()
.readTimeOut(8 * 1000) .readTimeOut(8 * 1000)
.writeTimeOut(8 * 1000) .writeTimeOut(8 * 1000)
...@@ -96,6 +149,7 @@ public class HttpUtils { ...@@ -96,6 +149,7 @@ public class HttpUtils {
}); });
} }
private void printLog(String url_str, Map<String, String> map) { private void printLog(String url_str, Map<String, String> map) {
try { try {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
...@@ -109,7 +163,7 @@ public class HttpUtils { ...@@ -109,7 +163,7 @@ public class HttpUtils {
} }
} }
public void download(String url_str, File file, Map<String, String> map, public void download(String url_str, File file,
final FileCallBack callback) { final FileCallBack callback) {
SobotNetLogUtils.i("下载地址:" + url_str); SobotNetLogUtils.i("下载地址:" + url_str);
SobotOkHttpUtils SobotOkHttpUtils
...@@ -139,9 +193,13 @@ public class HttpUtils { ...@@ -139,9 +193,13 @@ public class HttpUtils {
} }
public void uploadFile(Object cancelTag, String url_str, Map<String, String> map, public void uploadFile(Object cancelTag, String url_str, Map<String, String> paramMap, Map<String, String> headerMap,
String path, final StringCallBack callback) { String path, final StringCallBack callback) {
SobotNetLogUtils.i("请求URL: --> " + url_str);
SobotNetLogUtils.i("请求参数: --> " + paramMap);
if (headerMap == null) {
headerMap = new HashMap<>();
}
PostFormBuilder post = SobotOkHttpUtils.post(); PostFormBuilder post = SobotOkHttpUtils.post();
if (!TextUtils.isEmpty(path)) { if (!TextUtils.isEmpty(path)) {
File file = new File(path); File file = new File(path);
...@@ -149,9 +207,9 @@ public class HttpUtils { ...@@ -149,9 +207,9 @@ public class HttpUtils {
post.addFile("file", file.getName(), file); post.addFile("file", file.getName(), file);
} }
} }
post.url(url_str)// post.url(url_str)//
.params(map) .params(paramMap)
.headers(headerMap)
.tag(cancelTag) .tag(cancelTag)
.build() .build()
.connTimeOut(1000 * 30) .connTimeOut(1000 * 30)
...@@ -182,15 +240,18 @@ public class HttpUtils { ...@@ -182,15 +240,18 @@ public class HttpUtils {
* *
* @param msgTag 上传文件的识别码 这里相当于msgId * @param msgTag 上传文件的识别码 这里相当于msgId
* @param url_str 上传文件的地址 * @param url_str 上传文件的地址
* @param map 参数 * @param paramMap 参数
* @param path 文件本地地址 * @param path 文件本地地址
* @param imageFile 视频文件的快照本地地址 * @param imageFile 视频文件的快照本地地址
* @return * @return
*/ */
public SobotUploadTask addUploadFileTask(String msgTag, String url_str, Map<String, String> map, public SobotUploadTask addUploadFileTask(String msgTag, String url_str, Map<String, String> paramMap, Map<String, String> headerMap,
String path, String imageFile) { String path, String imageFile) {
SobotNetLogUtils.i("上传文件 请求URL: --> " + url_str); SobotNetLogUtils.i("上传文件 请求URL: --> " + url_str);
SobotNetLogUtils.i("上传文件 请求参数: --> " + map); SobotNetLogUtils.i("上传文件 请求参数: --> " + paramMap);
if (headerMap == null) {
headerMap = new HashMap<>();
}
PostFormBuilder post = SobotOkHttpUtils.post(); PostFormBuilder post = SobotOkHttpUtils.post();
if (!TextUtils.isEmpty(path)) { if (!TextUtils.isEmpty(path)) {
File file = new File(path); File file = new File(path);
...@@ -202,8 +263,12 @@ public class HttpUtils { ...@@ -202,8 +263,12 @@ public class HttpUtils {
post.addFile("imageFile", file2.getName(), file2); post.addFile("imageFile", file2.getName(), file2);
} }
if (headerMap == null) {
headerMap = new HashMap<>();
}
RequestCall requestCall = post.url(url_str)// RequestCall requestCall = post.url(url_str)//
.params(map) .params(paramMap)
.headers(headerMap)
.build() .build()
.connTimeOut(1000 * 30) .connTimeOut(1000 * 30)
.readTimeOut(1000 * 30) .readTimeOut(1000 * 30)
...@@ -221,25 +286,31 @@ public class HttpUtils { ...@@ -221,25 +286,31 @@ public class HttpUtils {
* *
* @param msgTag 上传文件的识别码 这里相当于msgId * @param msgTag 上传文件的识别码 这里相当于msgId
* @param url_str 文件的下载地址 * @param url_str 文件的下载地址
* @param map 参数 * @param paramMap 参数
* @return * @return
*/ */
public SobotDownloadTask addDownloadFileTask(String msgTag, String url_str, String fileName, Map<String, String> map) { public SobotDownloadTask addDownloadFileTask(String msgTag, String url_str, String fileName, Map<String, String> paramMap, Map<String, String> headerMap) {
if (TextUtils.isEmpty(msgTag) || TextUtils.isEmpty(url_str)) { if (TextUtils.isEmpty(msgTag) || TextUtils.isEmpty(url_str)) {
return null; return null;
} }
Random random = new Random(); Random random = new Random();
// msgid // msgid
return SobotDownload.request(msgTag, obtainGetRequest(url_str, map)) return SobotDownload.request(msgTag, obtainGetRequest(url_str, paramMap, headerMap))
.priority(random.nextInt(100)) .priority(random.nextInt(100))
.fileName(fileName) .fileName(fileName)
.save(); .save();
} }
public RequestCall obtainGetRequest(String url_str, Map<String, String> map) { public RequestCall obtainGetRequest(String url_str, Map<String, String> paramMap, Map<String, String> headerMap) {
SobotNetLogUtils.i("请求URL: --> " + url_str);
SobotNetLogUtils.i("请求参数: --> " + paramMap);
if (headerMap == null) {
headerMap = new HashMap<>();
}
return SobotOkHttpUtils.get().url(url_str) return SobotOkHttpUtils.get().url(url_str)
.params(map) .headers(headerMap)
.params(paramMap)
.build() .build()
.connTimeOut(1000 * 30) .connTimeOut(1000 * 30)
.readTimeOut(1000 * 30) .readTimeOut(1000 * 30)
...@@ -247,12 +318,18 @@ public class HttpUtils { ...@@ -247,12 +318,18 @@ public class HttpUtils {
} }
//同步 //同步
public Response doPostSync(Object cancelTag, String url_str, Map<String, String> map) throws IOException { public Response doPostSync(Object cancelTag, String url_str, Map<String, String> paramMap, Map<String, String> headerMap) throws IOException {
SobotNetLogUtils.i("请求URL: --> " + url_str);
SobotNetLogUtils.i("请求参数: --> " + paramMap);
if (headerMap == null) {
headerMap = new HashMap<>();
}
return SobotOkHttpUtils return SobotOkHttpUtils
.post() .post()
.tag(cancelTag) .tag(cancelTag)
.url(url_str) .url(url_str)
.params(map) .headers(headerMap)
.params(paramMap)
.build() .build()
.readTimeOut(8 * 1000) .readTimeOut(8 * 1000)
.writeTimeOut(8 * 1000) .writeTimeOut(8 * 1000)
...@@ -274,4 +351,13 @@ public class HttpUtils { ...@@ -274,4 +351,13 @@ public class HttpUtils {
void inProgress(int progress); void inProgress(int progress);
} }
public static String map2Json(Map<String, String> map) {
if (map != null && map.size() > 0) {
JSONObject a = new JSONObject(map);
return a.toString();
}
return "";
}
} }
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