Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
Sobot_module_Dev
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sobot_android
Sobot_module_Dev
Commits
440b8dd5
Commit
440b8dd5
authored
Dec 14, 2021
by
app_dev@sobot.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
sobot net 1.0.3
parent
29c28144
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
141 additions
and
39 deletions
+141
-39
sobot-network-publish-mavencentral.gradle
sobot_network/sobot-network-publish-mavencentral.gradle
+1
-1
SobotHttpUtils.java
.../main/java/com/sobot/network/apiUtils/SobotHttpUtils.java
+28
-12
HttpUtils.java
...twork/src/main/java/com/sobot/network/http/HttpUtils.java
+112
-26
No files found.
sobot_network/sobot-network-publish-mavencentral.gradle
View file @
440b8dd5
...
...
@@ -12,7 +12,7 @@ task androidSourcesJar(type: Jar) {
ext
{
PUBLISH_GROUP_ID
=
"com.sobot.library"
//项目包名
PUBLISH_ARTIFACT_ID
=
'net'
//项目名
PUBLISH_VERSION
=
'1.0.
1
'
//版本号
PUBLISH_VERSION
=
'1.0.
3
'
//版本号
}
...
...
sobot_network/src/main/java/com/sobot/network/apiUtils/SobotHttpUtils.java
View file @
440b8dd5
...
...
@@ -9,15 +9,16 @@ import com.sobot.network.http.log.SobotNetLogUtils;
import
java.io.IOException
;
import
java.util.Map
;
import
okhttp3.MediaType
;
import
okhttp3.Response
;
public
class
SobotHttpUtils
{
// ##################### 网络请求 #########################
public
static
void
doPost
(
Object
cancelTag
,
String
url
,
Map
<
String
,
String
>
m
ap
,
public
static
void
doPost
(
Object
cancelTag
,
String
url
,
Map
<
String
,
String
>
paramsM
ap
,
HttpUtils
.
StringCallBack
call
)
{
HttpUtils
.
getInstance
().
doPost
(
cancelTag
,
url
,
m
ap
,
null
,
call
);
HttpUtils
.
getInstance
().
doPost
(
cancelTag
,
url
,
paramsM
ap
,
null
,
call
);
}
public
static
void
doPost
(
Object
cancelTag
,
String
url
,
Map
<
String
,
String
>
paramsMap
,
Map
<
String
,
String
>
headerMap
,
...
...
@@ -25,14 +26,29 @@ public class SobotHttpUtils {
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
.
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
,
m
ap
,
call
);
HttpUtils
.
getInstance
().
doGet
(
cancelTag
,
url
,
paramsMap
,
headerM
ap
,
call
);
}
//同步请求
public
static
Response
doPostSync
(
Object
cancelTag
,
String
url
,
Map
<
String
,
String
>
m
ap
)
throws
IOException
{
return
HttpUtils
.
getInstance
().
doPostSync
(
cancelTag
,
url
,
m
ap
);
public
static
Response
doPostSync
(
Object
cancelTag
,
String
url
,
Map
<
String
,
String
>
paramsMap
,
Map
<
String
,
String
>
headerM
ap
)
throws
IOException
{
return
HttpUtils
.
getInstance
().
doPostSync
(
cancelTag
,
url
,
paramsMap
,
headerM
ap
);
}
...
...
@@ -40,24 +56,24 @@ public class SobotHttpUtils {
* 发送文件请求
*
* @param url
* @param
m
ap
* @param
paramsM
ap
* @param filePath
* @param callBack
*/
public
static
void
uploadFile
(
Object
cancelTag
,
String
url
,
Map
<
String
,
String
>
m
ap
,
public
static
void
uploadFile
(
Object
cancelTag
,
String
url
,
Map
<
String
,
String
>
paramsMap
,
Map
<
String
,
String
>
headerM
ap
,
final
String
filePath
,
HttpUtils
.
StringCallBack
callBack
)
{
///////////////////////////////////////////////////////
if
(
m
ap
!=
null
)
{
if
(
paramsM
ap
!=
null
)
{
// 基本传递参数
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
"sobot---请求参数: url = "
+
url
+
", filePath="
+
filePath
+
" "
);
for
(
String
key
:
m
ap
.
keySet
())
{
sb
.
append
(
key
+
"="
+
m
ap
.
get
(
key
)
+
", "
);
for
(
String
key
:
paramsM
ap
.
keySet
())
{
sb
.
append
(
key
+
"="
+
paramsM
ap
.
get
(
key
)
+
", "
);
}
SobotNetLogUtils
.
i
(
sb
.
toString
().
substring
(
0
,
sb
.
toString
().
length
()
-
2
));
}
HttpUtils
.
getInstance
().
uploadFile
(
cancelTag
,
url
,
m
ap
,
filePath
,
callBack
);
HttpUtils
.
getInstance
().
uploadFile
(
cancelTag
,
url
,
paramsMap
,
headerM
ap
,
filePath
,
callBack
);
}
...
...
sobot_network/src/main/java/com/sobot/network/http/HttpUtils.java
View file @
440b8dd5
...
...
@@ -11,12 +11,16 @@ import com.sobot.network.http.request.RequestCall;
import
com.sobot.network.http.upload.SobotUpload
;
import
com.sobot.network.http.upload.SobotUploadTask
;
import
org.json.JSONObject
;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Random
;
import
okhttp3.Call
;
import
okhttp3.MediaType
;
import
okhttp3.Response
;
public
class
HttpUtils
{
...
...
@@ -32,18 +36,19 @@ public class HttpUtils {
return
client
;
}
public
void
doPost
(
Object
cancelTag
,
final
String
url_str
,
final
Map
<
String
,
String
>
paramMap
,
final
Map
<
String
,
String
>
headerMap
,
public
void
doPost
(
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
.
size
()>
0
)
{
if
(
headerMap
==
null
)
{
headerMap
=
new
HashMap
<>();
}
SobotOkHttpUtils
.
post
()
.
tag
(
cancelTag
)
.
url
(
url_str
)
.
headers
(
headerMap
)
.
params
(
paramMap
)
.
build
()
.
readTimeOut
(
8
*
1000
)
...
...
@@ -66,15 +71,63 @@ public class HttpUtils {
});
}
public
void
doGet
(
Object
cancelTag
,
final
String
url_str
,
final
Map
<
String
,
String
>
map
,
final
StringCallBack
callback
)
{
public
void
doPostByString
(
Object
cancelTag
,
final
String
url_str
,
Map
<
String
,
String
>
paramMap
,
Map
<
String
,
String
>
headerMap
,
MediaType
mediaType
,
final
StringCallBack
callback
)
{
SobotNetLogUtils
.
i
(
"请求URL: --> "
+
url_str
);
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
(
"请求参数: --> "
+
map
);
SobotNetLogUtils
.
i
(
"请求参数: --> "
+
paramMap
);
if
(
headerMap
==
null
)
{
headerMap
=
new
HashMap
<>();
}
SobotOkHttpUtils
.
get
()
.
tag
(
cancelTag
)
.
url
(
url_str
)
.
params
(
map
)
.
params
(
paramMap
)
.
headers
(
headerMap
)
.
build
()
.
readTimeOut
(
8
*
1000
)
.
writeTimeOut
(
8
*
1000
)
...
...
@@ -96,6 +149,7 @@ public class HttpUtils {
});
}
private
void
printLog
(
String
url_str
,
Map
<
String
,
String
>
map
)
{
try
{
StringBuilder
sb
=
new
StringBuilder
();
...
...
@@ -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
)
{
SobotNetLogUtils
.
i
(
"下载地址:"
+
url_str
);
SobotOkHttpUtils
...
...
@@ -139,9 +193,13 @@ public class HttpUtils {
}
public
void
uploadFile
(
Object
cancelTag
,
String
url_str
,
Map
<
String
,
String
>
m
ap
,
public
void
uploadFile
(
Object
cancelTag
,
String
url_str
,
Map
<
String
,
String
>
paramMap
,
Map
<
String
,
String
>
headerM
ap
,
String
path
,
final
StringCallBack
callback
)
{
SobotNetLogUtils
.
i
(
"请求URL: --> "
+
url_str
);
SobotNetLogUtils
.
i
(
"请求参数: --> "
+
paramMap
);
if
(
headerMap
==
null
)
{
headerMap
=
new
HashMap
<>();
}
PostFormBuilder
post
=
SobotOkHttpUtils
.
post
();
if
(!
TextUtils
.
isEmpty
(
path
))
{
File
file
=
new
File
(
path
);
...
...
@@ -149,9 +207,9 @@ public class HttpUtils {
post
.
addFile
(
"file"
,
file
.
getName
(),
file
);
}
}
post
.
url
(
url_str
)
//
.
params
(
map
)
.
params
(
paramMap
)
.
headers
(
headerMap
)
.
tag
(
cancelTag
)
.
build
()
.
connTimeOut
(
1000
*
30
)
...
...
@@ -182,15 +240,18 @@ public class HttpUtils {
*
* @param msgTag 上传文件的识别码 这里相当于msgId
* @param url_str 上传文件的地址
* @param
map
参数
* @param
paramMap
参数
* @param path 文件本地地址
* @param imageFile 视频文件的快照本地地址
* @return
*/
public
SobotUploadTask
addUploadFileTask
(
String
msgTag
,
String
url_str
,
Map
<
String
,
String
>
m
ap
,
public
SobotUploadTask
addUploadFileTask
(
String
msgTag
,
String
url_str
,
Map
<
String
,
String
>
paramMap
,
Map
<
String
,
String
>
headerM
ap
,
String
path
,
String
imageFile
)
{
SobotNetLogUtils
.
i
(
"上传文件 请求URL: --> "
+
url_str
);
SobotNetLogUtils
.
i
(
"上传文件 请求参数: --> "
+
map
);
SobotNetLogUtils
.
i
(
"上传文件 请求参数: --> "
+
paramMap
);
if
(
headerMap
==
null
)
{
headerMap
=
new
HashMap
<>();
}
PostFormBuilder
post
=
SobotOkHttpUtils
.
post
();
if
(!
TextUtils
.
isEmpty
(
path
))
{
File
file
=
new
File
(
path
);
...
...
@@ -202,8 +263,12 @@ public class HttpUtils {
post
.
addFile
(
"imageFile"
,
file2
.
getName
(),
file2
);
}
if
(
headerMap
==
null
)
{
headerMap
=
new
HashMap
<>();
}
RequestCall
requestCall
=
post
.
url
(
url_str
)
//
.
params
(
map
)
.
params
(
paramMap
)
.
headers
(
headerMap
)
.
build
()
.
connTimeOut
(
1000
*
30
)
.
readTimeOut
(
1000
*
30
)
...
...
@@ -219,27 +284,33 @@ public class HttpUtils {
/**
* 添加下载文件任务
*
* @param msgTag 上传文件的识别码 这里相当于msgId
* @param url_str 文件的下载地址
* @param
map
参数
* @param msgTag
上传文件的识别码 这里相当于msgId
* @param url_str
文件的下载地址
* @param
paramMap
参数
* @return
*/
public
SobotDownloadTask
addDownloadFileTask
(
String
msgTag
,
String
url_str
,
String
fileName
,
Map
<
String
,
String
>
m
ap
)
{
public
SobotDownloadTask
addDownloadFileTask
(
String
msgTag
,
String
url_str
,
String
fileName
,
Map
<
String
,
String
>
paramMap
,
Map
<
String
,
String
>
headerM
ap
)
{
if
(
TextUtils
.
isEmpty
(
msgTag
)
||
TextUtils
.
isEmpty
(
url_str
))
{
return
null
;
}
Random
random
=
new
Random
();
// msgid
return
SobotDownload
.
request
(
msgTag
,
obtainGetRequest
(
url_str
,
m
ap
))
return
SobotDownload
.
request
(
msgTag
,
obtainGetRequest
(
url_str
,
paramMap
,
headerM
ap
))
.
priority
(
random
.
nextInt
(
100
))
.
fileName
(
fileName
)
.
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
)
.
params
(
map
)
.
headers
(
headerMap
)
.
params
(
paramMap
)
.
build
()
.
connTimeOut
(
1000
*
30
)
.
readTimeOut
(
1000
*
30
)
...
...
@@ -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
.
post
()
.
tag
(
cancelTag
)
.
url
(
url_str
)
.
params
(
map
)
.
headers
(
headerMap
)
.
params
(
paramMap
)
.
build
()
.
readTimeOut
(
8
*
1000
)
.
writeTimeOut
(
8
*
1000
)
...
...
@@ -274,4 +351,13 @@ public class HttpUtils {
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
""
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment