Skip to content

Commit 779e07a

Browse files
author
tangsiyuan
committed
update myokhttp README
1 parent e522fdb commit 779e07a

2 files changed

Lines changed: 119 additions & 3 deletions

File tree

MyOkhttp/README.md

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,111 @@
1-
## MyOkhttp Wiki
1+
## MyOkhttp Wiki
2+
3+
> 该模块对Okhttp3进行二次封装,对外提供了POST请求,GET请求,上传文件,下载文件,取消请求和Gson转换等功能.
4+
5+
### 1 POST请求
6+
7+
```java
8+
Map<String, String> params = new HashMap<String, String>();
9+
params.put("name", "tsy");
10+
11+
MyOkHttp.get().post(this, "http://192.168.3.1/test_okhttp.php", params, new JsonResponseHandler() {
12+
@Override
13+
public void onSuccess(int statusCode, JSONObject response) {
14+
LogUtils.v(TAG, statusCode + " " + response);
15+
}
16+
17+
@Override
18+
public void onFailure(int statusCode, String error_msg) {
19+
LogUtils.v(TAG, statusCode + " " + error_msg);
20+
}
21+
});
22+
```
23+
24+
### 2 GET请求
25+
26+
```java
27+
Map<String, String> params = new HashMap<String, String>();
28+
params.put("name", "tsy");
29+
30+
MyOkHttp.get().post(this, "http://192.168.3.1/test_okhttp.php", params, new GsonResponseHandler<BB>() {
31+
32+
@Override
33+
public void onFailure(int statusCode, String error_msg) {
34+
LogUtils.v(TAG, statusCode + " " + error_msg);
35+
}
36+
37+
@Override
38+
public void onSuccess(int statusCode, BB response) {
39+
LogUtils.v(TAG, statusCode + " " + response.ret);
40+
}
41+
});
42+
```
43+
44+
### 3 上传文件
45+
46+
```java
47+
Map<String, String> params = new HashMap<String, String>();
48+
params.put("name", "tsy");
49+
50+
Map<String, File> files = new HashMap<String, File>();
51+
File file = new File(Environment.getExternalStorageDirectory() + "/com.ci123.service.splashandroid/splash/1.png");
52+
files.put("avatar", file);
53+
54+
MyOkHttp.get().upload(this, "http://192.168.3.1/test_post.php", params, files, new GsonResponseHandler<BB>() {
55+
@Override
56+
public void onFailure(int statusCode, String error_msg) {
57+
LogUtils.v(TAG, statusCode + " " + error_msg);
58+
}
59+
60+
@Override
61+
public void onSuccess(int statusCode, BB response) {
62+
LogUtils.v(TAG, statusCode + " " + response.ret);
63+
}
64+
65+
@Override
66+
public void onProgress(long currentBytes, long totalBytes) {
67+
LogUtils.v(TAG, currentBytes + "/" + totalBytes);
68+
}
69+
});
70+
```
71+
72+
### 4 下载文件
73+
74+
```java
75+
MyOkHttp.get().download(this, "http://192.168.3.1/output_tmp.jpg",
76+
Environment.getExternalStorageDirectory() + "/com.tsy.splashandroid/", "1.jpg",
77+
new DownloadResponseHandler() {
78+
@Override
79+
public void onFinish(File download_file) {
80+
LogUtils.v(TAG, "onFinish:" + download_file.getPath());
81+
}
82+
83+
@Override
84+
public void onProgress(long currentBytes, long totalBytes) {
85+
LogUtils.v(TAG, currentBytes + "/" + totalBytes);
86+
}
87+
88+
@Override
89+
public void onFailure(String error_msg) {
90+
LogUtils.v(TAG, error_msg);
91+
}
92+
});
93+
```
94+
95+
### 5 取消请求(建议放在BaseActivity,BaseFragment的onDestroy中)
96+
97+
```java
98+
MyOkHttp.get().cancel(this);
99+
```
100+
101+
### 6 返回格式
102+
103+
post,get,upload3个接口可以选择返回格式为普通Json还是Gson
104+
105+
#### 6.1 普通json
106+
107+
回调继承JsonResponseHandler,例如POST请求的例子
108+
109+
#### 6.2 gson
110+
111+
回调继承GsonResponseHandler<T>,并设置泛型T,例如GET请求和Upload请求的例子

app/src/main/java/com/tsy/baseandroidproject/RootAct.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ protected void onCreate(Bundle savedInstanceState) {
3636

3737
//post请求
3838
private void doPost() {
39-
MyOkHttp.get().post(this, "http://192.168.3.1/test_okhttp.php", null, new JsonResponseHandler() {
39+
Map<String, String> params = new HashMap<String, String>();
40+
params.put("name", "tsy");
41+
42+
MyOkHttp.get().post(this, "http://192.168.3.1/test_okhttp.php", params, new JsonResponseHandler() {
4043
@Override
4144
public void onSuccess(int statusCode, JSONObject response) {
4245
LogUtils.v(TAG, statusCode + " " + response);
@@ -51,7 +54,10 @@ public void onFailure(int statusCode, String error_msg) {
5154

5255
//get请求
5356
private void doGet() {
54-
MyOkHttp.get().post(this, "http://192.168.3.1/test_okhttp.php", null, new GsonResponseHandler<BB>() {
57+
Map<String, String> params = new HashMap<String, String>();
58+
params.put("name", "tsy");
59+
60+
MyOkHttp.get().post(this, "http://192.168.3.1/test_okhttp.php", params, new GsonResponseHandler<BB>() {
5561

5662
@Override
5763
public void onFailure(int statusCode, String error_msg) {

0 commit comments

Comments
 (0)