Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
fastdevelop
Overview
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
高超
fastdevelop
Commits
32669913
Commit
32669913
authored
Jan 16, 2020
by
gao.chao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
网络框架进行代理模式改造
parent
f54efc6b
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
105 additions
and
51 deletions
+105
-51
app/src/main/java/com/mayi/demo/function/FunctionActivity.java
+2
-2
fastDevelop/src/main/java/com/mayi/fastdevelop/base/BaseApplication.java
+3
-0
fastDevelop/src/main/java/com/mayi/fastdevelop/http/HttpProxy.java
+44
-0
fastDevelop/src/main/java/com/mayi/fastdevelop/http/IHttp.java
+16
-0
fastDevelop/src/main/java/com/mayi/fastdevelop/http/NetWorkBuilder.java
+7
-7
fastDevelop/src/main/java/com/mayi/fastdevelop/http/ResultCallback.java
+1
-1
fastDevelop/src/main/java/com/mayi/fastdevelop/http/okhttp/HttpDns.java
+1
-1
fastDevelop/src/main/java/com/mayi/fastdevelop/http/okhttp/OKHttpModel.java
+31
-40
No files found.
app/src/main/java/com/mayi/demo/function/FunctionActivity.java
View file @
32669913
...
...
@@ -22,8 +22,8 @@ import com.mayi.fastdevelop.base.BaseActivity;
import
com.mayi.fastdevelop.bean.UserInfo
;
import
com.mayi.fastdevelop.comnon.Constant
;
import
com.mayi.fastdevelop.comnon.RequestCode
;
import
com.mayi.fastdevelop.
ok
http.NetWorkBuilder
;
import
com.mayi.fastdevelop.
ok
http.ResultCallback
;
import
com.mayi.fastdevelop.http.NetWorkBuilder
;
import
com.mayi.fastdevelop.http.ResultCallback
;
import
com.mayi.fastdevelop.util.LogUtils
;
import
com.mayi.fastdevelop.view.OnMultiClickListener
;
...
...
fastDevelop/src/main/java/com/mayi/fastdevelop/base/BaseApplication.java
View file @
32669913
...
...
@@ -10,6 +10,8 @@ import android.os.HandlerThread;
import
androidx.multidex.MultiDexApplication
;
import
com.mayi.fastdevelop.http.HttpProxy
;
import
com.mayi.fastdevelop.http.okhttp.OKHttpModel
;
import
com.mayi.fastdevelop.util.ActivitysManager
;
import
com.mayi.fastdevelop.util.DateUtil
;
import
com.mayi.fastdevelop.util.FileUtil
;
...
...
@@ -51,6 +53,7 @@ public abstract class BaseApplication extends MultiDexApplication {
initLog
();
initSmartRefreshLayout
();
MMKV
.
initialize
(
this
);
HttpProxy
.
getInstance
().
init
(
new
OKHttpModel
());
}
private
void
initSmartRefreshLayout
()
{
...
...
fastDevelop/src/main/java/com/mayi/fastdevelop/http/HttpProxy.java
0 → 100644
View file @
32669913
package
com
.
mayi
.
fastdevelop
.
http
;
import
java.io.File
;
public
class
HttpProxy
{
private
static
HttpProxy
instance
;
private
IHttp
iHttp
;
public
static
HttpProxy
getInstance
()
{
if
(
instance
==
null
)
{
instance
=
new
HttpProxy
();
}
return
instance
;
}
public
void
init
(
IHttp
iHttp
)
{
this
.
iHttp
=
iHttp
;
}
//上传文件
public
void
uploadFile
(
String
url
,
File
file
,
ResultCallback
callback
)
{
if
(
iHttp
!=
null
)
{
iHttp
.
uploadFile
(
url
,
file
,
callback
);
}
}
//发起get请求
public
void
performSynGetHttpRequest
(
String
url
,
ResultCallback
callback
)
{
if
(
iHttp
!=
null
)
{
iHttp
.
performSynGetHttpRequest
(
url
,
callback
);
}
}
// //发起post请求
public
void
performSynPostHttpRequest
(
String
url
,
String
json
,
ResultCallback
callback
)
{
if
(
iHttp
!=
null
)
{
iHttp
.
performSynPostHttpRequest
(
url
,
json
,
callback
);
}
}
}
fastDevelop/src/main/java/com/mayi/fastdevelop/http/IHttp.java
0 → 100644
View file @
32669913
package
com
.
mayi
.
fastdevelop
.
http
;
import
java.io.File
;
public
interface
IHttp
{
//上传文件
public
void
uploadFile
(
String
url
,
File
file
,
ResultCallback
callback
);
//发起get请求
public
void
performSynGetHttpRequest
(
String
url
,
ResultCallback
callback
);
//发起post请求
public
void
performSynPostHttpRequest
(
String
url
,
String
json
,
ResultCallback
callback
);
}
fastDevelop/src/main/java/com/mayi/fastdevelop/
ok
http/NetWorkBuilder.java
→
fastDevelop/src/main/java/com/mayi/fastdevelop/http/NetWorkBuilder.java
View file @
32669913
package
com
.
mayi
.
fastdevelop
.
ok
http
;
package
com
.
mayi
.
fastdevelop
.
http
;
import
android.text.TextUtils
;
...
...
@@ -71,18 +71,18 @@ public class NetWorkBuilder {
//发起post请求
private
void
sendPost
()
{
if
(!
TextUtils
.
isEmpty
(
stringParam
))
{
NetWorkUtil
.
getInstance
().
performSynPostHttpRequest
(
url
,
stringParam
,
callback
);
HttpProxy
.
getInstance
().
performSynPostHttpRequest
(
url
,
stringParam
,
callback
);
}
else
if
(
params
.
toString
().
length
()
>
2
)
{
NetWorkUtil
.
getInstance
().
performSynPostHttpRequest
(
url
,
params
.
toString
(),
callback
);
HttpProxy
.
getInstance
().
performSynPostHttpRequest
(
url
,
params
.
toString
(),
callback
);
}
else
{
NetWorkUtil
.
getInstance
().
performSynPostHttpRequest
(
url
,
null
,
callback
);
HttpProxy
.
getInstance
().
performSynPostHttpRequest
(
url
,
null
,
callback
);
}
}
//发起get请求
private
void
sendGet
()
{
if
(!
TextUtils
.
isEmpty
(
stringParam
))
{
NetWorkUtil
.
getInstance
().
performSynGetHttpRequest
(
url
+
"?"
+
stringParam
,
callback
);
HttpProxy
.
getInstance
().
performSynGetHttpRequest
(
url
+
"?"
+
stringParam
,
callback
);
}
else
if
(
params
.
toString
().
length
()
>
2
)
{
StringBuilder
builder
=
new
StringBuilder
();
builder
.
append
(
"?"
);
...
...
@@ -97,9 +97,9 @@ public class NetWorkBuilder {
}
String
s
=
builder
.
toString
();
s
.
substring
(
0
,
s
.
length
()
-
1
);
NetWorkUtil
.
getInstance
().
performSynGetHttpRequest
(
url
+
s
,
callback
);
HttpProxy
.
getInstance
().
performSynGetHttpRequest
(
url
+
s
,
callback
);
}
else
{
NetWorkUtil
.
getInstance
().
performSynGetHttpRequest
(
url
,
callback
);
HttpProxy
.
getInstance
().
performSynGetHttpRequest
(
url
,
callback
);
}
}
...
...
fastDevelop/src/main/java/com/mayi/fastdevelop/
ok
http/ResultCallback.java
→
fastDevelop/src/main/java/com/mayi/fastdevelop/http/ResultCallback.java
View file @
32669913
package
com
.
mayi
.
fastdevelop
.
ok
http
;
package
com
.
mayi
.
fastdevelop
.
http
;
import
java.lang.reflect.ParameterizedType
;
...
...
fastDevelop/src/main/java/com/mayi/fastdevelop/okhttp/HttpDns.java
→
fastDevelop/src/main/java/com/mayi/fastdevelop/
http/
okhttp/HttpDns.java
View file @
32669913
package
com
.
mayi
.
fastdevelop
.
okhttp
;
package
com
.
mayi
.
fastdevelop
.
http
.
okhttp
;
import
java.net.InetAddress
;
import
java.net.UnknownHostException
;
...
...
fastDevelop/src/main/java/com/mayi/fastdevelop/
okhttp/NetWorkUti
l.java
→
fastDevelop/src/main/java/com/mayi/fastdevelop/
http/okhttp/OKHttpMode
l.java
View file @
32669913
package
com
.
mayi
.
fastdevelop
.
okhttp
;
package
com
.
mayi
.
fastdevelop
.
http
.
okhttp
;
import
android.os.Handler
;
import
android.os.Looper
;
import
android.text.TextUtils
;
import
com.alibaba.fastjson.JSON
;
import
com.mayi.fastdevelop.http.IHttp
;
import
com.mayi.fastdevelop.http.ResultCallback
;
import
com.mayi.fastdevelop.util.LogUtils
;
import
com.tencent.bugly.crashreport.CrashReport
;
...
...
@@ -12,6 +14,7 @@ import org.json.JSONException;
import
org.json.JSONObject
;
import
java.io.BufferedReader
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
...
...
@@ -28,29 +31,44 @@ import okhttp3.RequestBody;
import
okhttp3.Response
;
import
okio.Buffer
;
public
class
OKHttpModel
implements
IHttp
{
@Override
public
void
uploadFile
(
String
url
,
File
file
,
ResultCallback
callback
)
{
}
public
class
NetWorkUtil
{
@Override
public
void
performSynGetHttpRequest
(
String
url
,
ResultCallback
callback
)
{
Request
request
=
new
Request
.
Builder
().
get
()
.
url
(
url
)
.
build
();
initiateRequest
(
callback
,
request
);
}
@Override
public
void
performSynPostHttpRequest
(
String
url
,
String
json
,
ResultCallback
callback
)
{
if
(
TextUtils
.
isEmpty
(
json
))
{
json
=
""
;
}
RequestBody
body
=
RequestBody
.
create
(
FORM_CONTENT_TYPE
,
json
);
Request
request
=
new
Request
.
Builder
()
.
url
(
url
)
.
post
(
body
)
.
build
();
initiateRequest
(
callback
,
request
);
}
private
static
NetWorkUtil
instance
;
private
OkHttpClient
client
;
private
final
MediaType
FORM_CONTENT_TYPE
=
MediaType
.
parse
(
"application/json;charset=utf-8"
);
private
Handler
handler
;
p
rivate
NetWorkUti
l
()
{
p
ublic
OKHttpMode
l
()
{
client
=
new
OkHttpClient
.
Builder
()
.
connectTimeout
(
60
,
TimeUnit
.
SECONDS
)
.
readTimeout
(
60
,
TimeUnit
.
SECONDS
).
dns
(
new
HttpDns
()).
build
();
handler
=
new
Handler
(
Looper
.
getMainLooper
());
}
public
static
NetWorkUtil
getInstance
()
{
if
(
instance
==
null
)
{
instance
=
new
NetWorkUtil
();
}
return
instance
;
}
/**
* 将InputStream转换为String
*
...
...
@@ -110,20 +128,6 @@ public class NetWorkUtil {
// }
// }
//发起get请求
public
void
performSynGetHttpRequest
(
String
url
,
ResultCallback
callback
)
{
Request
request
=
new
Request
.
Builder
().
get
()
.
url
(
url
)
.
addHeader
(
"content-type"
,
"application/json"
)
.
addHeader
(
"__REQUEST_TYPE"
,
"HTTP_CLIENT"
)
.
addHeader
(
"AUTH_TYPE"
,
"1"
)
.
addHeader
(
"AUTH_TOKEN"
,
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ7XCJ1c2VyQ29kZVwiOlwiOTA0NjM5NlwifSIsImlhdCI6MTU2MTQ1MTQwOX0.xizAX3iHVFMXNElpdMILZIYD7K7x8gaF2dG9J1hHPhE94e5a8fbea974e10aeebbe55eee21845"
)
.
addHeader
(
"x-auth-token"
,
"5fea181d-ec7f-4083-8647-3afa22268627"
)
.
addHeader
(
"USER_CODE"
,
"9046396"
)
.
build
();
initiateRequest
(
callback
,
request
);
}
private
void
initiateRequest
(
final
ResultCallback
callback
,
final
Request
request
)
{
client
.
newCall
(
request
).
enqueue
(
new
Callback
()
{
@Override
...
...
@@ -144,7 +148,7 @@ public class NetWorkUtil {
}
Throwable
throwable
=
new
Throwable
(
"网络异常"
,
e
);
CrashReport
.
postCatchedException
(
throwable
);
LogUtils
.
netWorkFail
(
"
NetWorkUtil
"
,
call
.
request
().
url
().
toString
(),
bodyToString
(
call
.
request
()),
e
);
LogUtils
.
netWorkFail
(
"
HttpProxy
"
,
call
.
request
().
url
().
toString
(),
bodyToString
(
call
.
request
()),
e
);
}
@Override
...
...
@@ -185,24 +189,11 @@ public class NetWorkUtil {
}
});
}
LogUtils
.
netWorkSuccess
(
"
NetWorkUtil
"
,
call
.
request
().
url
().
toString
(),
bodyToString
(
call
.
request
()),
json
);
LogUtils
.
netWorkSuccess
(
"
HttpProxy
"
,
call
.
request
().
url
().
toString
(),
bodyToString
(
call
.
request
()),
json
);
}
});
}
//发起post请求
public
void
performSynPostHttpRequest
(
String
url
,
String
json
,
ResultCallback
callback
)
{
if
(
TextUtils
.
isEmpty
(
json
))
{
json
=
""
;
}
RequestBody
body
=
RequestBody
.
create
(
FORM_CONTENT_TYPE
,
json
);
Request
request
=
new
Request
.
Builder
()
.
url
(
url
)
.
post
(
body
)
.
build
();
initiateRequest
(
callback
,
request
);
}
private
boolean
isText
(
MediaType
mediaType
)
{
if
(
mediaType
.
type
()
!=
null
&&
mediaType
.
type
().
equals
(
"text"
))
{
return
true
;
...
...
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