GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 版本升级1.4.1,修改post请求部分方法,增加全局拦截器和指定请求拦截器接口,修复demo编译失败问题 · uncle-byte/OkHttpUtils@9a6520a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • README.md
  • app
    • build.gradle
    • src/main
      • java/com/lzy/okhttpdemo
        • activity
          • CustomRequestActivity.java
          • FileDownloadActivity.java
          • PostTextActivity.java
        • utils
          • Urls.java
      • res/layout
        • activity_post_text.xml
  • build.gradle
  • lib
    • okhttpserver-0.1.4.jar
    • okhttputils-1.4.1.jar
  • okhttpserver
    • build.gradle
  • okhttputils_v1.4.0.apk
  • okhttputils_v1.4.1.apk
  • okhttputils
    • bintray.gradle
    • build.gradle
    • src/main/java/com/lzy/okhttputils
      • OkHttpUtils.java
      • request
        • BaseRequest.java
        • PostRequest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎README.md‎

Copy file name to clipboardExpand all lines: README.md
+23-9Lines changed: 23 additions & 9 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
* 对于Android Studio的用户,可以选择添加:
2828
```java
29-
compile 'com.lzy.net:okhttputils:1.4.0' //可以单独使用,不需要依赖下方的扩展包
29+
compile 'com.lzy.net:okhttputils:1.4.1' //可以单独使用,不需要依赖下方的扩展包
3030
compile 'com.lzy.net:okhttpserver:0.1.4' //扩展了下载管理和上传管理,根据需要添加
3131

3232
compile 'com.lzy.net:okhttputils:+' //版本号使用 + 可以自动引用最新版
@@ -39,7 +39,7 @@
3939
```
4040
* 对于Eclipse的用户,可以选择添加 `/lib` 目录下的:
4141
```java
42-
okhttputils-1.4.0.jar
42+
okhttputils-1.4.1.jar
4343
okhttpserver-0.1.4.jar
4444
```
4545

@@ -93,7 +93,8 @@
9393
.setReadTimeOut(OkHttpUtils.DEFAULT_MILLISECONDS) //全局的读取超时时间
9494
.setWriteTimeOut(OkHttpUtils.DEFAULT_MILLISECONDS) //全局的写入超时时间
9595
.addCommonHeaders(headers) //设置全局公共头
96-
.addCommonParams(params); //设置全局公共参数
96+
.addCommonParams(params) //设置全局公共参数
97+
.addInterceptor(interceptor); //添加自定义拦截器
9798
}
9899
```
99100

@@ -138,18 +139,30 @@ OkHttpUtils.get(Urls.URL_DOWNLOAD)//
138139
```java
139140
OkHttpUtils.post(Urls.URL_TEXT_UPLOAD)//
140141
.tag(this)//
141-
.content("这是要上传的长文本数据!")//
142-
.mediaType(PostRequest.MEDIA_TYPE_PLAIN)//
142+
.postString("这是要上传的长文本数据!")//
143143
.execute(new StringCallback() {
144144
@Override
145145
public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
146146
//上传成功
147147
}
148148
});
149149
```
150-
如果要上传Json,把上面的 mediaType 改为 `.mediaType(PostRequest.MEDIA_TYPE_JSON)`
151150

152-
### 5.请求功能的所有配置讲解
151+
### 5.普通Post,直接上传Json类型的文本
152+
不建议这么用,该方法上传字符串会清空实体中其他所有的参数,但头信息不清除
153+
```java
154+
OkHttpUtils.post(Urls.URL_TEXT_UPLOAD)//
155+
.tag(this)//
156+
.postJson("{\"des\": \"这里面要写标准的json格式数据\"}")//
157+
.execute(new StringCallback() {
158+
@Override
159+
public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
160+
//上传成功
161+
}
162+
});
163+
```
164+
165+
### 6.请求功能的所有配置讲解
153166

154167
以下代码包含了以下内容:
155168

@@ -167,6 +180,7 @@ OkHttpUtils.get(Urls.URL_METHOD) // 请求方式和请求url, get请求不需要
167180
.cacheKey("cacheKey") // 设置当前请求的缓存key,建议每个不同功能的请求设置一个
168181
.cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST) // 缓存模式,详细请看第四部分,缓存介绍
169182
.setCertificates(getAssets().open("srca.cer")) // 自签名https的证书,可变参数,可以设置多个
183+
.addInterceptor(interceptor) // 添加自定义拦截器
170184
.headers("header1", "headerValue1") // 添加请求头参数
171185
.headers("header2", "headerValue2") // 支持多请求头参数同时添加
172186
.params("param1", "paramValue1") // 添加请求参数
@@ -232,7 +246,7 @@ OkHttpUtils.get(Urls.URL_METHOD) // 请求方式和请求url, get请求不需要
232246
}
233247
});
234248
```
235-
### 6.取消请求
249+
### 7.取消请求
236250
每个请求前都设置了一个参数`tag`,取消则通过` OkHttpUtils.cancel(tag)`执行。
237251
例如:在Activity中,当Activity销毁取消请求,可以在onDestory里面统一取消。
238252
```java
@@ -244,7 +258,7 @@ OkHttpUtils.get(Urls.URL_METHOD) // 请求方式和请求url, get请求不需要
244258
OkHttpUtils.getInstance().cancelTag(this);
245259
}
246260
```
247-
### 7.同步的请求
261+
### 8.同步的请求
248262
execute方法不传入callback即为同步的请求,返回`Response`对象,需要自己解析
249263
```java
250264
Response response = OkHttpUtils.get("http://www.baidu.com")//
Collapse file

‎app/build.gradle‎

Copy file name to clipboardExpand all lines: app/build.gradle
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
applicationId "com.lzy.okhttpdemo"
99
minSdkVersion 14
1010
targetSdkVersion 22
11-
versionCode 14
12-
versionName "1.4.0"
11+
versionCode 15
12+
versionName "1.4.1"
1313
}
1414
buildTypes {
1515
release {
Collapse file

‎app/src/main/java/com/lzy/okhttpdemo/activity/CustomRequestActivity.java‎

Copy file name to clipboardExpand all lines: app/src/main/java/com/lzy/okhttpdemo/activity/CustomRequestActivity.java
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ protected void onDestroy() {
3636
super.onDestroy();
3737
//Activity销毁时,取消网络请求
3838
OkHttpUtils.getInstance().cancelTag(this);
39-
4039
}
4140

4241
@OnClick(R.id.requestJson)
Collapse file

‎app/src/main/java/com/lzy/okhttpdemo/activity/FileDownloadActivity.java‎

Copy file name to clipboardExpand all lines: app/src/main/java/com/lzy/okhttpdemo/activity/FileDownloadActivity.java
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.lzy.okhttputils.request.BaseRequest;
1818

1919
import java.io.File;
20+
import java.nio.ByteBuffer;
2021

2122
import butterknife.Bind;
2223
import butterknife.ButterKnife;
Collapse file

‎app/src/main/java/com/lzy/okhttpdemo/activity/PostTextActivity.java‎

Copy file name to clipboardExpand all lines: app/src/main/java/com/lzy/okhttpdemo/activity/PostTextActivity.java
+11-1Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,17 @@ public void postString(View view) {
5151
.tag(this)//
5252
.headers("header1", "headerValue1")//
5353
.params("param1", "paramValue1")//
54-
.content("这是要上传的长文本数据!")//
54+
.postString("这是要上传的长文本数据!")//
55+
.execute(new TextCallBack<>(this, RequestInfo.class));
56+
}
57+
58+
@OnClick(R.id.postBytes)
59+
public void postBytes(View view) {
60+
OkHttpUtils.post(Urls.URL_TEXT_UPLOAD)//
61+
.tag(this)//
62+
.headers("header1", "headerValue1")//
63+
.params("param1", "paramValue1")//
64+
.postBytes("这是字节数据".getBytes())//
5565
.mediaType(PostRequest.MEDIA_TYPE_PLAIN)//
5666
.execute(new TextCallBack<>(this, RequestInfo.class));
5767
}
Collapse file

‎app/src/main/java/com/lzy/okhttpdemo/utils/Urls.java‎

Copy file name to clipboardExpand all lines: app/src/main/java/com/lzy/okhttpdemo/utils/Urls.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
public class Urls {
1313
public static final String SERVER = "http://server.jeasonlzy.com/OkHttpUtils/";
14-
// public static final String SERVER = "http://192.168.1.101:8080/OkHttpUtils/";
14+
// public static final String SERVER = "http://192.168.1.102:8080/OkHttpUtils/";
1515
public static final String URL_METHOD = SERVER + "method";
1616
public static final String URL_CACHE = SERVER + "cache";
1717
public static final String URL_IMAGE = SERVER + "image";
Collapse file

‎app/src/main/res/layout/activity_post_text.xml‎

Copy file name to clipboardExpand all lines: app/src/main/res/layout/activity_post_text.xml
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@
1919
android:text="向服务器Post上传String字符串"
2020
android:textAllCaps="false"/>
2121

22+
<Button
23+
android:id="@+id/postBytes"
24+
android:layout_width="match_parent"
25+
android:layout_height="wrap_content"
26+
android:text="向服务器Post上传其他数据"
27+
android:textAllCaps="false"/>
28+
2229
</LinearLayout>
Collapse file

‎build.gradle‎

Copy file name to clipboardExpand all lines: build.gradle
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.0.0'
8+
classpath 'com.android.tools.build:gradle:2.1.0'
99

1010
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
1111
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
Collapse file

‎lib/okhttpserver-0.1.4.jar‎

Copy file name to clipboard
0 Bytes
Binary file not shown.
Collapse file

‎lib/okhttputils-1.4.0.jar‎ ‎lib/okhttputils-1.4.1.jar‎lib/okhttputils-1.4.0.jar renamed to lib/okhttputils-1.4.1.jar

Copy file name to clipboard
61.2 KB
Binary file not shown.

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.