Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mmm-rain
AndroidUtilCode
提交
0791ebc8
A
AndroidUtilCode
项目概览
mmm-rain
/
AndroidUtilCode
与 Fork 源项目一致
从无法访问的项目Fork
通知
4
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
AndroidUtilCode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
0791ebc8
编写于
2月 11, 2019
作者:
B
Blankj
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
See 02/11 log
上级
f563ed8c
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
372 addition
and
18 deletion
+372
-18
lib/base/src/main/java/com/blankj/lib/base/BaseActivity.kt
lib/base/src/main/java/com/blankj/lib/base/BaseActivity.kt
+4
-2
subutil/lib/src/main/java/com/blankj/subutil/util/HttpsUtil.java
.../lib/src/main/java/com/blankj/subutil/util/HttpsUtil.java
+1
-1
utilcode/lib/src/main/java/com/blankj/utilcode/util/HttpUtils.java
...lib/src/main/java/com/blankj/utilcode/util/HttpUtils.java
+297
-0
utilcode/lib/src/main/java/com/blankj/utilcode/util/KeyboardUtils.java
...src/main/java/com/blankj/utilcode/util/KeyboardUtils.java
+3
-5
utilcode/lib/src/main/java/com/blankj/utilcode/util/RomUtils.java
.../lib/src/main/java/com/blankj/utilcode/util/RomUtils.java
+3
-3
utilcode/lib/src/main/java/com/blankj/utilcode/util/Utils.java
...ode/lib/src/main/java/com/blankj/utilcode/util/Utils.java
+3
-5
utilcode/lib/src/test/java/com/blankj/utilcode/util/BaseTest.java
.../lib/src/test/java/com/blankj/utilcode/util/BaseTest.java
+0
-2
utilcode/lib/src/test/java/com/blankj/utilcode/util/HttpUtilsTest.java
...src/test/java/com/blankj/utilcode/util/HttpUtilsTest.java
+61
-0
未找到文件。
lib/base/src/main/java/com/blankj/lib/base/BaseActivity.kt
浏览文件 @
0791ebc8
...
...
@@ -5,7 +5,10 @@ import android.os.Bundle
import
android.support.v7.app.AppCompatActivity
import
android.view.LayoutInflater
import
android.view.View
import
com.blankj.utilcode.util.*
import
com.blankj.utilcode.util.AntiShakeUtils
import
com.blankj.utilcode.util.AppUtils
import
com.blankj.utilcode.util.ToastUtils
import
com.blankj.utilcode.util.Utils
import
com.r0adkll.slidr.Slidr
/**
...
...
@@ -60,6 +63,5 @@ abstract class BaseActivity : AppCompatActivity(), IBaseView {
override
fun
onDestroy
()
{
super
.
onDestroy
()
AppUtils
.
unregisterAppStatusChangedListener
(
this
)
KeyboardUtils
.
fixSoftInputLeaks
(
this
);
}
}
\ No newline at end of file
subutil/lib/src/main/java/com/blankj/subutil/util/HttpsUtil.java
浏览文件 @
0791ebc8
...
...
@@ -10,7 +10,7 @@ import java.util.Scanner;
/**
* Create by MilkZS on 2019/1/9 13:36
*/
public
class
HttpsUtil
{
public
final
class
HttpsUtil
{
private
static
final
int
CONNECT_TIMEOUT_TIME
=
15000
;
private
static
final
int
READ_TIMEOUT_TIME
=
19000
;
...
...
utilcode/lib/src/main/java/com/blankj/utilcode/util/HttpUtils.java
0 → 100644
浏览文件 @
0791ebc8
package
com.blankj.utilcode.util
;
import
android.accounts.NetworkErrorException
;
import
android.annotation.SuppressLint
;
import
android.support.annotation.NonNull
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.UnsupportedEncodingException
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.nio.charset.Charset
;
import
java.security.cert.X509Certificate
;
import
java.util.HashMap
;
import
java.util.Map
;
import
javax.net.ssl.HostnameVerifier
;
import
javax.net.ssl.HttpsURLConnection
;
import
javax.net.ssl.SSLContext
;
import
javax.net.ssl.SSLSession
;
import
javax.net.ssl.TrustManager
;
import
javax.net.ssl.X509TrustManager
;
/**
* <pre>
* author: blankj
* blog : http://blankj.com
* time : 2019/02/08
* desc : utils about http
* </pre>
*/
public
class
HttpUtils
{
private
static
final
int
CONNECT_TIMEOUT_TIME
=
15000
;
private
static
final
int
READ_TIMEOUT_TIME
=
19000
;
private
static
final
TrustManager
[]
DEFAULT_TRUST_MANAGERS
=
new
TrustManager
[]{
new
X509TrustManager
()
{
@Override
public
X509Certificate
[]
getAcceptedIssuers
()
{
return
null
;
}
@SuppressLint
(
"TrustAllX509TrustManager"
)
@Override
public
void
checkClientTrusted
(
X509Certificate
[]
chain
,
String
authType
)
{
/**/
}
@SuppressLint
(
"TrustAllX509TrustManager"
)
@Override
public
void
checkServerTrusted
(
X509Certificate
[]
chain
,
String
authType
)
{
/**/
}
}
};
private
static
final
HostnameVerifier
DEFAULT_VERIFIER
=
new
HostnameVerifier
()
{
@SuppressLint
(
"BadHostnameVerifier"
)
@Override
public
boolean
verify
(
String
hostname
,
SSLSession
session
)
{
return
true
;
}
};
private
static
final
Config
CONFIG
=
new
Config
();
private
HttpUtils
()
{
throw
new
UnsupportedOperationException
(
"u can't instantiate me..."
);
}
public
static
void
doGet
(
@NonNull
final
Request
request
,
final
BaseResponse
response
)
{
if
(
response
==
null
)
return
;
HttpURLConnection
conn
=
null
;
try
{
conn
=
getConnection
(
request
,
"GET"
);
int
responseCode
=
conn
.
getResponseCode
();
if
(
responseCode
==
200
)
{
InputStream
is
=
conn
.
getInputStream
();
response
.
onSuccess
(
is
);
is
.
close
();
}
else
if
(
responseCode
==
301
||
responseCode
==
302
)
{
String
location
=
conn
.
getHeaderField
(
"Location"
);
doGet
(
request
,
response
);
}
else
{
response
.
onError
(
new
NetworkErrorException
(
"Response code: "
+
responseCode
));
}
}
catch
(
IOException
e
)
{
response
.
onError
(
e
);
}
finally
{
if
(
conn
!=
null
)
{
conn
.
disconnect
();
}
}
}
public
static
void
doPost
(
@NonNull
final
Request
request
,
final
BaseResponse
response
)
{
if
(
response
==
null
)
return
;
HttpURLConnection
conn
=
null
;
try
{
conn
=
getConnection
(
request
,
"POST"
);
addHeader
(
conn
,
request
.
header
);
// conn.setRequestProperty("accept", "*/*");
// conn.setRequestProperty("connection", "Keep-Alive");
// conn.setRequestProperty("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
// conn.setRequestProperty("content-length", String.valueOf(sb.length()));
// conn.setDoOutput(true);
//
// PrintWriter printWriter = new PrintWriter(conn.getOutputStream());
// printWriter.write(sb.toString());
// printWriter.flush();
// printWriter.close();
int
responseCode
=
conn
.
getResponseCode
();
if
(
responseCode
==
200
)
{
InputStream
is
=
conn
.
getInputStream
();
response
.
onSuccess
(
is
);
is
.
close
();
}
else
{
response
.
onError
(
new
NetworkErrorException
(
"Response code: "
+
responseCode
));
}
}
catch
(
IOException
e
)
{
response
.
onError
(
e
);
}
finally
{
if
(
conn
!=
null
)
{
conn
.
disconnect
();
}
}
}
private
static
HttpURLConnection
getConnection
(
final
Request
request
,
final
String
requestMethod
)
throws
IOException
{
URL
httpUrl
=
new
URL
(
request
.
url
);
HttpURLConnection
conn
=
(
HttpURLConnection
)
httpUrl
.
openConnection
();
if
(
conn
instanceof
HttpsURLConnection
)
{
HttpsURLConnection
httpsConn
=
(
HttpsURLConnection
)
conn
;
trustAllHosts
(
httpsConn
);
}
conn
.
setConnectTimeout
(
CONFIG
.
connectTimeout
);
conn
.
setReadTimeout
(
CONFIG
.
readTimeout
);
conn
.
setUseCaches
(
CONFIG
.
useCaches
);
conn
.
setRequestMethod
(
requestMethod
);
return
conn
;
}
private
static
void
trustAllHosts
(
HttpsURLConnection
conn
)
{
try
{
SSLContext
sslContext
=
SSLContext
.
getInstance
(
"TLS"
);
sslContext
.
init
(
null
,
DEFAULT_TRUST_MANAGERS
,
new
java
.
security
.
SecureRandom
());
conn
.
setSSLSocketFactory
(
sslContext
.
getSocketFactory
());
conn
.
setHostnameVerifier
(
DEFAULT_VERIFIER
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
private
static
void
addHeader
(
final
HttpURLConnection
conn
,
final
Map
<
String
,
String
>
headerMap
)
{
if
(
headerMap
!=
null
)
{
for
(
String
key
:
headerMap
.
keySet
())
{
conn
.
setRequestProperty
(
key
,
headerMap
.
get
(
key
));
}
}
}
private
static
boolean
isSpace
(
final
String
s
)
{
if
(
s
==
null
)
return
true
;
for
(
int
i
=
0
,
len
=
s
.
length
();
i
<
len
;
++
i
)
{
if
(!
Character
.
isWhitespace
(
s
.
charAt
(
i
)))
{
return
false
;
}
}
return
true
;
}
public
static
class
Config
{
private
int
connectTimeout
=
CONNECT_TIMEOUT_TIME
;
private
int
readTimeout
=
READ_TIMEOUT_TIME
;
private
boolean
useCaches
=
false
;
}
public
static
class
Request
{
private
String
url
;
private
Map
<
String
,
String
>
header
;
private
RequestBody
body
;
public
static
Request
withUrl
(
@NonNull
final
String
url
)
{
return
new
Request
(
url
);
}
private
Request
(
final
String
url
)
{
this
.
url
=
url
;
}
public
Request
addHeader
(
@NonNull
final
String
name
,
@NonNull
final
String
value
)
{
if
(
header
==
null
)
{
header
=
new
HashMap
<>();
}
header
.
put
(
name
,
value
);
return
this
;
}
public
Request
addHeader
(
@NonNull
final
Map
<
String
,
String
>
header
)
{
if
(
this
.
header
==
null
)
{
this
.
header
=
new
HashMap
<>();
}
this
.
header
.
putAll
(
header
);
return
this
;
}
public
Request
post
(
@NonNull
final
RequestBody
body
)
{
this
.
body
=
body
;
return
this
;
}
}
public
static
final
class
RequestBody
{
String
mediaType
;
byte
[]
content
;
long
length
;
private
RequestBody
(
String
mediaType
,
byte
[]
content
)
{
this
.
mediaType
=
mediaType
;
this
.
content
=
content
;
length
=
content
==
null
?
0
:
content
.
length
;
}
private
static
String
getCharsetFromMediaType
(
String
mediaType
)
{
mediaType
=
mediaType
.
toLowerCase
().
replace
(
" "
,
""
);
int
index
=
mediaType
.
indexOf
(
"charset="
);
if
(
index
==
-
1
)
return
"utf-8"
;
int
st
=
index
+
8
;
int
end
=
mediaType
.
length
();
for
(
int
i
=
st
;
i
<
end
;
i
++)
{
char
c
=
mediaType
.
charAt
(
i
);
if
((
c
<
'a'
||
c
>
'z'
)
&&
(
c
<
'0'
||
c
>
'9'
)
&&
c
!=
'-'
)
{
end
=
i
;
break
;
}
}
if
(
st
<
end
)
{
String
charset
=
mediaType
.
substring
(
st
,
end
);
if
(
Charset
.
isSupported
(
charset
))
return
charset
;
}
throw
new
IllegalArgumentException
(
"MediaType is not correct: \""
+
mediaType
+
"\""
);
}
public
static
RequestBody
create
(
String
mediaType
,
byte
[]
content
)
{
return
new
RequestBody
(
mediaType
,
content
);
}
public
static
RequestBody
form
(
final
Map
<
String
,
String
>
form
)
{
return
form
(
form
,
"utf-8"
);
}
public
static
RequestBody
form
(
final
Map
<
String
,
String
>
form
,
String
charset
)
{
String
mediaType
=
"application/x-www-form-urlencoded;charset="
+
charset
;
if
(
form
!=
null
)
{
final
StringBuilder
sb
=
new
StringBuilder
();
for
(
String
key
:
form
.
keySet
())
{
if
(
sb
.
length
()
!=
0
)
{
sb
.
append
(
"&"
);
}
sb
.
append
(
key
).
append
(
"="
).
append
(
form
.
get
(
key
));
}
try
{
return
new
RequestBody
(
mediaType
,
sb
.
toString
().
getBytes
(
charset
));
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
();
}
}
return
new
RequestBody
(
mediaType
,
null
);
}
public
static
RequestBody
json
(
final
String
json
)
{
return
json
(
json
,
"utf-8"
);
}
public
static
RequestBody
json
(
final
String
json
,
String
charset
)
{
String
mediaType
=
"application/json;charset="
+
charset
;
if
(
json
!=
null
)
{
try
{
return
new
RequestBody
(
mediaType
,
json
.
getBytes
(
charset
));
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
();
}
}
return
new
RequestBody
(
mediaType
,
null
);
}
public
static
RequestBody
file
(
String
mediaType
,
final
File
file
)
{
return
file
(
mediaType
,
file
);
}
}
public
static
abstract
class
BaseResponse
{
public
abstract
void
onSuccess
(
InputStream
is
);
public
abstract
void
onError
(
Throwable
t
);
}
}
utilcode/lib/src/main/java/com/blankj/utilcode/util/KeyboardUtils.java
浏览文件 @
0791ebc8
...
...
@@ -267,8 +267,8 @@ public final class KeyboardUtils {
(
InputMethodManager
)
Utils
.
getApp
().
getSystemService
(
Context
.
INPUT_METHOD_SERVICE
);
if
(
imm
==
null
)
return
;
String
[]
leakViews
=
new
String
[]{
"mLastSrvView"
,
"mCurRootView"
,
"mServedView"
,
"mNextServedView"
};
for
(
String
leakView
:
leakViews
)
{
try
{
try
{
for
(
String
leakView
:
leakViews
)
{
Field
leakViewField
=
InputMethodManager
.
class
.
getDeclaredField
(
leakView
);
if
(
leakViewField
==
null
)
continue
;
if
(!
leakViewField
.
isAccessible
())
{
...
...
@@ -280,10 +280,8 @@ public final class KeyboardUtils {
if
(
view
.
getRootView
()
==
activity
.
getWindow
().
getDecorView
().
getRootView
())
{
leakViewField
.
set
(
imm
,
null
);
}
}
catch
(
Throwable
th
)
{
th
.
printStackTrace
();
}
}
}
catch
(
Throwable
ignore
)
{
/**/
}
}
/**
...
...
utilcode/lib/src/main/java/com/blankj/utilcode/util/RomUtils.java
浏览文件 @
0791ebc8
...
...
@@ -42,7 +42,7 @@ public final class RomUtils {
private
static
final
String
[]
ROM_HTC
=
{
"htc"
};
private
static
final
String
[]
ROM_SONY
=
{
"sony"
};
private
static
final
String
[]
ROM_GIONEE
=
{
"gionee"
,
"amigo"
};
private
static
final
String
[]
ROM_MOTOROLA
=
{
"motorola"
};
private
static
final
String
[]
ROM_MOTOROLA
=
{
"motorola"
};
private
static
final
String
VERSION_PROPERTY_HUAWEI
=
"ro.build.version.emui"
;
private
static
final
String
VERSION_PROPERTY_VIVO
=
"ro.vivo.os.build.display.id"
;
...
...
@@ -449,8 +449,8 @@ public final class RomUtils {
@Override
public
String
toString
()
{
return
"RomInfo{name
:
"
+
name
+
"
\nversion:
"
+
version
+
"}"
;
return
"RomInfo{name
=
"
+
name
+
"
, version=
"
+
version
+
"}"
;
}
}
}
\ No newline at end of file
utilcode/lib/src/main/java/com/blankj/utilcode/util/Utils.java
浏览文件 @
0791ebc8
...
...
@@ -341,8 +341,8 @@ public final class Utils {
(
InputMethodManager
)
Utils
.
getApp
().
getSystemService
(
Context
.
INPUT_METHOD_SERVICE
);
if
(
imm
==
null
)
return
;
String
[]
leakViews
=
new
String
[]{
"mLastSrvView"
,
"mCurRootView"
,
"mServedView"
,
"mNextServedView"
};
for
(
String
leakView
:
leakViews
)
{
try
{
try
{
for
(
String
leakView
:
leakViews
)
{
Field
leakViewField
=
InputMethodManager
.
class
.
getDeclaredField
(
leakView
);
if
(
leakViewField
==
null
)
continue
;
if
(!
leakViewField
.
isAccessible
())
{
...
...
@@ -354,10 +354,8 @@ public final class Utils {
if
(
view
.
getRootView
()
==
activity
.
getWindow
().
getDecorView
().
getRootView
())
{
leakViewField
.
set
(
imm
,
null
);
}
}
catch
(
Throwable
th
)
{
th
.
printStackTrace
();
}
}
}
catch
(
Throwable
ignore
)
{
/**/
}
}
}
...
...
utilcode/lib/src/test/java/com/blankj/utilcode/util/BaseTest.java
浏览文件 @
0791ebc8
...
...
@@ -29,8 +29,6 @@ public class BaseTest {
@Test
public
void
test
()
throws
Exception
{
// final CountDownLatch countDownLatch = new CountDownLatch(1);
// final Scanner scanner = new Scanner(System.in);
// ExecutorService singlePool = ThreadUtils.getSinglePool();
...
...
utilcode/lib/src/test/java/com/blankj/utilcode/util/HttpUtilsTest.java
0 → 100644
浏览文件 @
0791ebc8
package
com.blankj.utilcode.util
;
import
org.junit.Test
;
/**
* <pre>
* author: blankj
* blog : http://blankj.com
* time : 2019/02/10
* desc :
* </pre>
*/
public
class
HttpUtilsTest
extends
BaseTest
{
@Test
public
void
doGet
()
{
// HttpUtils.doGet("http://raw.githubusercontent.com/Blankj/AndroidUtilCode/85bc44d1c8adb31027870ea4cb7a931700c80cad/LICENSE", new HttpUtils.BaseResponse() {
// @Override
// public void onSuccess(InputStream is) {
// LogUtils.json(ConvertUtils.inputStream2String(is, "UTF-8"));
// }
//
// @Override
// public void onError(Throwable t) {
// LogUtils.e(t);
// }
// });
}
@Test
public
void
doPost
()
{
// HashMap<String, String> map = new HashMap<>();
// map.put("username", "blankj");
// map.put("password", "blankj");
// HttpUtils.doPost("http://www.wanandroid.com/user/login",
// map,
// new HttpUtils.BaseResponse() {
// @Override
// public void onSuccess(InputStream is) {
// LogUtils.json(ConvertUtils.inputStream2String(is, "UTF-8"));
// }
//
// @Override
// public void onError(Throwable t) {
// LogUtils.e(t);
// }
// });
// HttpUtils.doGet("http://www.wanandroid.com/lg/collect/list/0/json",
// new HttpUtils.BaseResponse() {
// @Override
// public void onSuccess(InputStream is) {
// LogUtils.json(ConvertUtils.inputStream2String(is, "UTF-8"));
// }
//
// @Override
// public void onError(Throwable t) {
// LogUtils.e(t);
// }
// });
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录