Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
门心叼龙
okhttp3-example
提交
f9fc6603
O
okhttp3-example
项目概览
门心叼龙
/
okhttp3-example
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
O
okhttp3-example
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
f9fc6603
编写于
5月 13, 2020
作者:
门心叼龙
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
code perfect
上级
d7efd9fd
变更
11
显示空白变更内容
内联
并排
Showing
11 changed file
with
160 addition
and
41 deletion
+160
-41
okhttp3-java-example/app/build.gradle
okhttp3-java-example/app/build.gradle
+2
-3
okhttp3-java-example/app/src/main/AndroidManifest.xml
okhttp3-java-example/app/src/main/AndroidManifest.xml
+5
-5
okhttp3-java-example/app/src/main/java/com/mxdl/okhttp3/Main2Activity.java
...ple/app/src/main/java/com/mxdl/okhttp3/Main2Activity.java
+104
-0
okhttp3-java-example/app/src/main/java/com/mxdl/okhttp3/MainActivity.java
...mple/app/src/main/java/com/mxdl/okhttp3/MainActivity.java
+3
-4
okhttp3-java-example/app/src/main/java/com/mxdl/okhttp3/bean/User.java
...example/app/src/main/java/com/mxdl/okhttp3/bean/User.java
+15
-15
okhttp3-java-example/app/src/main/java/com/mxdl/okhttp3/ok_fly/FlyHttpUtil.java
...pp/src/main/java/com/mxdl/okhttp3/ok_fly/FlyHttpUtil.java
+3
-7
okhttp3-java-example/app/src/main/java/com/mxdl/okhttp3/ok_fly/request/PostRequest.java
...ain/java/com/mxdl/okhttp3/ok_fly/request/PostRequest.java
+1
-1
okhttp3-java-example/app/src/main/java/com/mxdl/okhttp3/ok_fly/uml/okhttp.puml
...app/src/main/java/com/mxdl/okhttp3/ok_fly/uml/okhttp.puml
+0
-0
okhttp3-java-example/app/src/main/java/com/mxdl/okhttp3/ok_man/OkHttpManager.java
.../src/main/java/com/mxdl/okhttp3/ok_man/OkHttpManager.java
+1
-1
okhttp3-java-example/app/src/main/java/com/mxdl/okhttp3/ok_man/response/MyCallBack.java
...ain/java/com/mxdl/okhttp3/ok_man/response/MyCallBack.java
+3
-5
okhttp3-java-example/app/src/main/res/layout/activity_main2.xml
...3-java-example/app/src/main/res/layout/activity_main2.xml
+23
-0
未找到文件。
okhttp3-java-example/app/build.gradle
浏览文件 @
f9fc6603
...
...
@@ -26,11 +26,10 @@ dependencies {
testImplementation
'junit:junit:4.12'
androidTestImplementation
'androidx.test.ext:junit:1.1.1'
androidTestImplementation
'androidx.test.espresso:espresso-core:3.2.0'
implementation
'com.facebook.stetho:stetho:1.5.1'
implementation
(
"com.squareup.okhttp3:okhttp:3.12.10"
)
implementation
'com.facebook.stetho:stetho:1.5.1'
implementation
'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation
(
'com.github.ihsanbal:LoggingInterceptor:3.1.0-rc5'
)
{
implementation
(
'com.github.ihsanbal:LoggingInterceptor:3.0.0'
)
{
exclude
group:
'org.json'
,
module:
'json'
}
implementation
group:
'com.google.code.gson'
,
name:
'gson'
,
version:
'2.8.6'
...
...
okhttp3-java-example/app/src/main/AndroidManifest.xml
浏览文件 @
f9fc6603
...
...
@@ -3,16 +3,16 @@
package=
"com.mxdl.okhttp3"
>
<uses-permission
android:name=
"android.permission.INTERNET"
/>
<application
android:name=
".MyApplication"
android:allowBackup=
"true"
android:icon=
"@mipmap/ic_launcher"
android:label=
"@string/app_name"
android:roundIcon=
"@mipmap/ic_launcher_round"
android:supportsRtl=
"true"
android:theme=
"@style/AppTheme"
android:networkSecurityConfig=
"@xml/network_security_config"
android:name=
".MyApplication"
>
<activity
android:name=
".MainActivity"
>
android:usesCleartextTraffic=
"true"
android:theme=
"@style/AppTheme"
>
<activity
android:name=
".MainActivity"
></activity>
<activity
android:name=
".Main2Activity"
>
<intent-filter>
<action
android:name=
"android.intent.action.MAIN"
/>
...
...
okhttp3-java-example/app/src/main/java/com/mxdl/okhttp3/Main2Activity.java
0 → 100644
浏览文件 @
f9fc6603
package
com.mxdl.okhttp3
;
import
androidx.appcompat.app.AppCompatActivity
;
import
android.os.Bundle
;
import
android.os.Handler
;
import
android.util.Log
;
import
android.view.View
;
import
android.widget.TextView
;
import
com.google.gson.Gson
;
import
com.mxdl.okhttp3.bean.User
;
import
java.io.IOException
;
import
javax.annotation.Nullable
;
import
javax.net.ssl.HandshakeCompletedListener
;
import
okhttp3.Call
;
import
okhttp3.Callback
;
import
okhttp3.MediaType
;
import
okhttp3.OkHttpClient
;
import
okhttp3.Request
;
import
okhttp3.RequestBody
;
import
okhttp3.Response
;
import
okio.BufferedSink
;
public
class
Main2Activity
extends
AppCompatActivity
{
private
TextView
txtContent
;
private
Handler
handler
;
@Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
setContentView
(
R
.
layout
.
activity_main2
);
txtContent
=
findViewById
(
R
.
id
.
txt_content
);
handler
=
new
Handler
();
}
public
void
login
(
View
view
)
{
Log
.
v
(
"MYTAG"
,
"login start..."
);
new
Thread
(
new
Runnable
()
{
@Override
public
void
run
()
{
//1.创建一个HttpClient
OkHttpClient
httpClient
=
new
OkHttpClient
.
Builder
().
build
();
//2.创建一个request请求
String
url
=
"http://192.168.0.189:8080/user/login?username=mxdl&password=123456"
;
Request
loginRequst
=
new
Request
.
Builder
().
url
(
url
).
build
();
//3.创建一个请求命令
Call
loginCall
=
httpClient
.
newCall
(
loginRequst
);
//4.发起一个同步的请求
try
{
Response
loginResponse
=
loginCall
.
execute
();
Log
.
v
(
"MYTAG"
,
loginResponse
.
body
().
string
());
}
catch
(
IOException
e
)
{
Log
.
v
(
"MYTAG"
,
e
.
toString
());
}
}
}).
start
();
}
public
void
addUser
(
View
view
)
{
Log
.
v
(
"MYTAG"
,
"addUser start..."
);
//1.创建一个http客户端
OkHttpClient
httpClient
=
new
OkHttpClient
.
Builder
().
build
();
//2.创建一个用户注册的请求
User
user
=
new
User
(
"zhangsan"
,
123
);
Request
addUserRequest
=
new
Request
.
Builder
()
.
url
(
"http://192.168.0.189:8080/user/addUser"
)
.
post
(
RequestBody
.
create
(
MediaType
.
parse
(
"application/json;charset=utf-8"
),
new
Gson
().
toJson
(
user
)))
.
build
();
//3.创建一个请求命令
Call
addUserCall
=
httpClient
.
newCall
(
addUserRequest
);
//4.执行用户注册的命令
addUserCall
.
enqueue
(
new
Callback
()
{
@Override
public
void
onFailure
(
Call
call
,
IOException
e
)
{
Log
.
v
(
"MYTAG"
,
"onFail start..."
);
}
@Override
public
void
onResponse
(
Call
call
,
Response
response
)
throws
IOException
{
Log
.
v
(
"MYTAG"
,
"onResponse start..."
);
final
String
content
=
response
.
body
().
string
();
Log
.
v
(
"MYTAG"
,
content
);
//更新UI操作
handler
.
post
(
new
Runnable
()
{
@Override
public
void
run
()
{
txtContent
.
setText
(
content
);
}
});
}
});
}
}
okhttp3-java-example/app/src/main/java/com/mxdl/okhttp3/MainActivity.java
浏览文件 @
f9fc6603
...
...
@@ -27,7 +27,6 @@ import com.mxdl.okhttp3.ok_chain.utils.L;
import
com.mxdl.okhttp3.response.AddUserResponse
;
import
com.mxdl.okhttp3.response.BaseResponse
;
import
org.jetbrains.annotations.NotNull
;
import
java.io.IOException
;
import
java.util.HashMap
;
...
...
@@ -165,20 +164,20 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
OkHttpClient
client
=
new
OkHttpClient
();
//构建一个request请求
final
Request
request
=
new
Request
.
Builder
().
url
(
"http://192.168.31.105:8080/user/addUser"
)
.
post
(
RequestBody
.
create
(
"{\"userName\":\"mxdl\",\"passWord\":123456}"
,
MediaType
.
parse
(
"application/json;charset=utf-8"
)
))
.
post
(
RequestBody
.
create
(
MediaType
.
parse
(
"application/json;charset=utf-8"
),
"{\"userName\":\"mxdl\",\"passWord\":123456}"
))
.
build
();
//根据request创建一个call命令
Call
call
=
client
.
newCall
(
request
);
//执行call命令
call
.
enqueue
(
new
Callback
()
{
@Override
public
void
onFailure
(
@NotNull
Call
call
,
@NotNull
IOException
e
)
{
public
void
onFailure
(
Call
call
,
IOException
e
)
{
Log
.
v
(
"MYTAG"
,
"onFail start..."
);
Log
.
v
(
"MTTAG"
,
e
.
toString
());
}
@Override
public
void
onResponse
(
@NotNull
Call
call
,
@NotNull
Response
response
)
throws
IOException
{
public
void
onResponse
(
Call
call
,
Response
response
)
throws
IOException
{
Log
.
v
(
"MYTAG"
,
"onSucc start..."
);
Log
.
v
(
"MYTAG"
,
response
.
body
().
string
());
}
...
...
okhttp3-java-example/app/src/main/java/com/mxdl/okhttp3/bean/User.java
浏览文件 @
f9fc6603
package
com.mxdl.okhttp3.bean
;
public
class
User
{
private
String
user
N
ame
;
private
int
pass
W
ord
;
private
String
user
n
ame
;
private
int
pass
w
ord
;
public
User
(
String
user
Name
,
int
passW
ord
)
{
this
.
user
Name
=
userN
ame
;
this
.
pass
Word
=
passW
ord
;
public
User
(
String
user
name
,
int
passw
ord
)
{
this
.
user
name
=
usern
ame
;
this
.
pass
word
=
passw
ord
;
}
public
String
getUser
N
ame
()
{
return
user
N
ame
;
public
String
getUser
n
ame
()
{
return
user
n
ame
;
}
public
void
setUser
Name
(
String
userN
ame
)
{
this
.
user
Name
=
userN
ame
;
public
void
setUser
name
(
String
usern
ame
)
{
this
.
user
name
=
usern
ame
;
}
public
int
getPass
W
ord
()
{
return
pass
W
ord
;
public
int
getPass
w
ord
()
{
return
pass
w
ord
;
}
public
void
setPass
Word
(
int
passW
ord
)
{
this
.
pass
Word
=
passW
ord
;
public
void
setPass
word
(
int
passw
ord
)
{
this
.
pass
word
=
passw
ord
;
}
@Override
public
String
toString
()
{
return
"User{"
+
"user
Name='"
+
userN
ame
+
'\''
+
", pass
Word="
+
passW
ord
+
"user
name='"
+
usern
ame
+
'\''
+
", pass
word="
+
passw
ord
+
'}'
;
}
}
okhttp3-java-example/app/src/main/java/com/mxdl/okhttp3/ok_fly/FlyHttpUtil.java
浏览文件 @
f9fc6603
...
...
@@ -9,13 +9,9 @@ import com.mxdl.okhttp3.ok_fly.builder.GetBuilder;
import
com.mxdl.okhttp3.ok_fly.builder.PostBuilder
;
import
com.mxdl.okhttp3.ok_fly.call.RequestCall
;
import
com.mxdl.okhttp3.ok_fly.callback.RequestListener
;
import
org.jetbrains.annotations.NotNull
;
import
java.io.IOException
;
import
java.lang.reflect.ParameterizedType
;
import
java.util.concurrent.Executor
;
import
okhttp3.Call
;
import
okhttp3.Callback
;
import
okhttp3.OkHttpClient
;
...
...
@@ -64,12 +60,12 @@ public class FlyHttpUtil {
}
requestCall
.
getCall
().
enqueue
(
new
Callback
()
{
@Override
public
void
onFailure
(
@NotNull
Call
call
,
@NotNull
IOException
e
)
{
public
void
onFailure
(
Call
call
,
IOException
e
)
{
sendFail
(
e
,
listener
);
}
@Override
public
void
onResponse
(
@NotNull
Call
call
,
@NotNull
Response
response
)
throws
IOException
{
public
void
onResponse
(
Call
call
,
Response
response
)
throws
IOException
{
if
(
call
.
isCanceled
())
{
sendFail
(
new
IOException
(
"request cancel"
),
listener
);
return
;
...
...
@@ -87,7 +83,7 @@ public class FlyHttpUtil {
});
}
private
void
sendFail
(
@NotNull
final
IOException
e
,
final
RequestListener
listener
)
{
private
void
sendFail
(
final
IOException
e
,
final
RequestListener
listener
)
{
mExecutor
.
execute
(
new
Runnable
()
{
@Override
public
void
run
()
{
...
...
okhttp3-java-example/app/src/main/java/com/mxdl/okhttp3/ok_fly/request/PostRequest.java
浏览文件 @
f9fc6603
...
...
@@ -26,7 +26,7 @@ public class PostRequest extends BaseRequest {
@Override
public
Request
buildRequest
()
{
if
(
body
!=
null
){
builder
.
post
(
RequestBody
.
create
(
new
Gson
().
toJson
(
body
),
MediaType
.
parse
(
"application/json;charset=utf-8"
)));
builder
.
post
(
RequestBody
.
create
(
MediaType
.
parse
(
"application/json;charset=utf-8"
),
new
Gson
().
toJson
(
body
)));
}
return
builder
.
build
();
}
...
...
okhttp3-java-example/app/src/main/java/com/mxdl/okhttp3/ok_
chain
/uml/okhttp.puml
→
okhttp3-java-example/app/src/main/java/com/mxdl/okhttp3/ok_
fly
/uml/okhttp.puml
浏览文件 @
f9fc6603
文件已移动
okhttp3-java-example/app/src/main/java/com/mxdl/okhttp3/ok_man/OkHttpManager.java
浏览文件 @
f9fc6603
...
...
@@ -50,7 +50,7 @@ public class OkHttpManager {
new
Request
.
Builder
()
.
url
(
url
)
.
post
(
RequestBody
.
create
(
jsonBody
,
MediaType
.
parse
(
"application/json;charset=utf-8"
)
))
.
post
(
RequestBody
.
create
(
MediaType
.
parse
(
"application/json;charset=utf-8"
),
jsonBody
))
.
build
())
.
enqueue
(
callback
);
...
...
okhttp3-java-example/app/src/main/java/com/mxdl/okhttp3/ok_man/response/MyCallBack.java
浏览文件 @
f9fc6603
...
...
@@ -8,8 +8,6 @@ import androidx.annotation.NonNull;
import
com.google.gson.Gson
;
import
org.jetbrains.annotations.NotNull
;
import
java.io.IOException
;
import
java.lang.reflect.ParameterizedType
;
...
...
@@ -73,7 +71,7 @@ public class MyCallBack<T> implements Callback {
}
@Override
public
void
onFailure
(
@NotNull
Call
call
,
@NotNull
IOException
e
)
{
public
void
onFailure
(
Call
call
,
IOException
e
)
{
Message
message
=
new
Message
();
message
.
what
=
ON_FAIL
;
message
.
obj
=
e
;
...
...
@@ -82,7 +80,7 @@ public class MyCallBack<T> implements Callback {
}
@Override
public
void
onResponse
(
@NotNull
Call
call
,
@NotNull
Response
response
)
{
public
void
onResponse
(
Call
call
,
Response
response
)
{
try
{
if
(
response
.
isSuccessful
())
{
Message
message
=
new
Message
();
...
...
@@ -116,7 +114,7 @@ public class MyCallBack<T> implements Callback {
return
tClass
;
}
//
@NotNull
//
// private Type getType() {
// Type type = mOnResponse.getClass().getGenericSuperclass();
// Type[] types = ((ParameterizedType) type).getActualTypeArguments();
...
...
okhttp3-java-example/app/src/main/res/layout/activity_main2.xml
0 → 100644
浏览文件 @
f9fc6603
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:orientation=
"vertical"
xmlns:android=
"http://schemas.android.com/apk/res/android"
>
<Button
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:text=
"同步的Get请求"
android:onClick=
"login"
/>
<Button
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:text=
"异步的Post请求"
android:onClick=
"addUser"
/>
<TextView
android:id=
"@+id/txt_content"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
/>
</LinearLayout>
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录