Skip to content

Commit 28668d4

Browse files
committed
新增上传文件请求示例
新增支持请求失败重试机制 修复极速下载逻辑的 Bug 优化 Http 处理器参数对象 优化表单提交和文件提交的逻辑
1 parent 5a456f5 commit 28668d4

38 files changed

+941
-512
lines changed

EasyHttp.apk

-99.7 KB
Binary file not shown.

EasyHttp.jpg

-5.33 KB
Loading

README.md

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#### Gradle 集成
88

99
dependencies {
10-
implementation 'com.hjq:http:2.0'
10+
implementation 'com.hjq:http:3.0'
1111
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
1212
implementation 'com.google.code.gson:gson:2.8.5'
1313
}
@@ -51,6 +51,8 @@
5151
.setServer(server)
5252
// 设置请求处理策略
5353
.setHandler(new RequestHandler())
54+
// 设置请求重试次数
55+
.setRetryCount(3)
5456
// 添加全局请求参数
5557
//.addParam("token", "6666666")
5658
// 添加全局请求头
@@ -97,24 +99,7 @@
9799

98100
* implements IRequestPath:实现这个接口之后可以重新指定这个请求的接口路径
99101

100-
#### Get 请求
101-
102-
EasyHttp.get(this)
103-
.api(new LoginApi()
104-
.setUserName("Android 轮子哥")
105-
.setPassword("123456"))
106-
.request(new OnHttpListener<HttpData<LoginBean>>() {
107-
108-
@Override
109-
public void onSucceed(HttpData<LoginBean> data) {
110-
ToastUtils.show("登录成功");
111-
}
112-
113-
@Override
114-
public void onFail(Exception e) {}
115-
});
116-
117-
#### Post 请求
102+
#### 发起请求
118103

119104
EasyHttp.post(this)
120105
.api(new LoginApi()
@@ -235,7 +220,7 @@
235220

236221
* 悬浮窗框架:[XToast](https://github.com/getActivity/XToast)
237222

238-
#### 特别鸣谢
223+
#### 特别感谢
239224

240225
[张鸿洋](https://github.com/hongyangAndroid)
241226

app/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
applicationId "com.hjq.http.demo"
88
minSdkVersion 14
99
targetSdkVersion 28
10-
versionCode 20
11-
versionName "2.0"
10+
versionCode 30
11+
versionName "3.0"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}
1414
buildTypes {
@@ -43,5 +43,5 @@ dependencies {
4343
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
4444

4545
// 内存泄漏捕捉:https://github.com/square/leakcanary
46-
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0-beta-3'
46+
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0'
4747
}

app/src/main/AndroidManifest.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
1414
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
1515

16+
<!-- 安装包的权限 -->
17+
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
18+
1619
<application
1720
android:name=".MyApplication"
1821
android:icon="@mipmap/ic_launcher"
@@ -21,6 +24,18 @@
2124
android:roundIcon="@mipmap/ic_launcher_round"
2225
android:theme="@style/AppTheme"
2326
tools:targetApi="n">
27+
28+
<!-- 适配 Android 7.0 文件意图 -->
29+
<provider
30+
android:name="android.support.v4.content.FileProvider"
31+
android:authorities="${applicationId}.provider"
32+
android:exported="false"
33+
android:grantUriPermissions="true">
34+
<meta-data
35+
android:name="android.support.FILE_PROVIDER_PATHS"
36+
android:resource="@xml/file_paths" />
37+
</provider>
38+
2439
<activity android:name=".MainActivity">
2540
<intent-filter>
2641
<action android:name="android.intent.action.MAIN" />

app/src/main/java/com/hjq/http/demo/MainActivity.java

Lines changed: 124 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
11
package com.hjq.http.demo;
22

3+
import android.content.Context;
4+
import android.content.Intent;
5+
import android.graphics.Bitmap;
6+
import android.graphics.drawable.BitmapDrawable;
7+
import android.graphics.drawable.Drawable;
8+
import android.net.Uri;
9+
import android.os.Build;
310
import android.os.Bundle;
411
import android.os.Environment;
12+
import android.support.v4.content.ContextCompat;
13+
import android.support.v4.content.FileProvider;
514
import android.view.View;
615
import android.widget.ProgressBar;
716

817
import com.hjq.http.EasyHttp;
918
import com.hjq.http.demo.http.model.HttpData;
1019
import com.hjq.http.demo.http.request.SearchAuthorApi;
1120
import com.hjq.http.demo.http.request.SearchBlogsApi;
21+
import com.hjq.http.demo.http.request.UpdateImageApi;
1222
import com.hjq.http.demo.http.response.SearchBean;
1323
import com.hjq.http.listener.OnDownloadListener;
1424
import com.hjq.http.listener.OnHttpListener;
15-
import com.hjq.http.model.DownloadTask;
25+
import com.hjq.http.model.DownloadInfo;
1626
import com.hjq.http.model.HttpMethod;
1727
import com.hjq.permissions.OnPermission;
1828
import com.hjq.permissions.Permission;
1929
import com.hjq.permissions.XXPermissions;
2030
import com.hjq.toast.ToastUtils;
2131

32+
import org.json.JSONObject;
33+
2234
import java.io.File;
35+
import java.io.FileOutputStream;
36+
import java.io.IOException;
2337
import java.util.List;
2438

25-
39+
/**
40+
* author : Android 轮子哥
41+
* github : https://github.com/getActivity/EasyHttp
42+
* time : 2019/05/19
43+
* desc : 网络请求示例
44+
*/
2645
public class MainActivity extends BaseActivity implements View.OnClickListener, OnPermission {
2746

2847
private ProgressBar mProgressBar;
@@ -36,6 +55,7 @@ protected void onCreate(Bundle savedInstanceState) {
3655

3756
findViewById(R.id.btn_main_get).setOnClickListener(this);
3857
findViewById(R.id.btn_main_post).setOnClickListener(this);
58+
findViewById(R.id.btn_main_update).setOnClickListener(this);
3959
findViewById(R.id.btn_main_download).setOnClickListener(this);
4060

4161
requestPermission();
@@ -72,7 +92,7 @@ protected void onRestart() {
7292
super.onRestart();
7393
if (XXPermissions.isHasPermission(this, Permission.Group.STORAGE)) {
7494
hasPermission(null, true);
75-
}else {
95+
} else {
7696
requestPermission();
7797
}
7898
}
@@ -87,12 +107,14 @@ public void onClick(View v) {
87107
.request(new OnHttpListener<HttpData<SearchBean>>() {
88108

89109
@Override
90-
public void onSucceed(HttpData<SearchBean> data) {
110+
public void onSucceed(HttpData<SearchBean> result) {
91111
ToastUtils.show("请求成功");
92112
}
93113

94114
@Override
95-
public void onFail(Exception e) {}
115+
public void onFail(Exception e) {
116+
ToastUtils.show(e.getMessage());
117+
}
96118
});
97119
break;
98120
case R.id.btn_main_post:
@@ -102,42 +124,66 @@ public void onFail(Exception e) {}
102124
.request(new OnHttpListener<HttpData<SearchBean>>() {
103125

104126
@Override
105-
public void onSucceed(HttpData<SearchBean> data) {
127+
public void onSucceed(HttpData<SearchBean> result) {
106128
ToastUtils.show("请求成功");
107129
}
108130

109131
@Override
110-
public void onFail(Exception e) {}
132+
public void onFail(Exception e) {
133+
ToastUtils.show(e.getMessage());
134+
}
135+
});
136+
break;
137+
case R.id.btn_main_update:
138+
File file = new File(Environment.getExternalStorageDirectory(), getString(R.string.app_name) + ".png");
139+
// 生成图片到本地
140+
drawableToFile(ContextCompat.getDrawable(this, R.mipmap.ic_launcher), file);
141+
142+
EasyHttp.post(this)
143+
.api(new UpdateImageApi()
144+
.setImage(file))
145+
.request(new OnHttpListener<JSONObject>() {
146+
147+
@Override
148+
public void onSucceed(JSONObject result) {
149+
ToastUtils.show("上传成功");
150+
}
151+
152+
@Override
153+
public void onFail(Exception e) {
154+
ToastUtils.show(e.getMessage());
155+
}
111156
});
112157
break;
113158
case R.id.btn_main_download:
114159
EasyHttp.download(this)
115160
.method(HttpMethod.GET)
116-
.file(new File(Environment.getExternalStorageDirectory(), "手机QQ.apk"))
117-
.url("https://qd.myapp.com/myapp/qqteam/AndroidQQ/mobileqq_android.apk")
118-
.md5("47CBDF2A2940B7773DD1B63CBCFD86E1")
119-
//.url("http://dldir1.qq.com/weixin/android/weixin708android1540.apk")
161+
.file(new File(Environment.getExternalStorageDirectory(), "微信.apk"))
162+
//.url("https://qd.myapp.com/myapp/qqteam/AndroidQQ/mobileqq_android.apk")
163+
.url("http://dldir1.qq.com/weixin/android/weixin708android1540.apk")
164+
.md5("2E8BDD7686474A7BC4A51ADC3667CABF")
120165
.listener(new OnDownloadListener() {
121166

122167
@Override
123-
public void onDownloadStart(DownloadTask task) {
168+
public void onDownloadStart(DownloadInfo info) {
124169
mProgressBar.setVisibility(View.VISIBLE);
125-
ToastUtils.show("下载开始:" + task.getFile().getName());
170+
ToastUtils.show("下载开始:" + info.getFile().getName());
126171
}
127172

128173
@Override
129-
public void onDownloadProgress(DownloadTask task) {
130-
mProgressBar.setProgress(task.getProgress());
174+
public void onDownloadProgress(DownloadInfo info) {
175+
mProgressBar.setProgress(info.getDownloadProgress());
131176
}
132177

133178
@Override
134-
public void onDownloadComplete(DownloadTask task) {
179+
public void onDownloadComplete(DownloadInfo info) {
135180
mProgressBar.setVisibility(View.GONE);
136-
ToastUtils.show("下载完成:" + task.getFile().getPath());
181+
ToastUtils.show("下载完成:" + info.getFile().getPath());
182+
installApk(MainActivity.this, info.getFile());
137183
}
138184

139185
@Override
140-
public void onDownloadError(DownloadTask task, Exception e) {
186+
public void onDownloadError(DownloadInfo info, Exception e) {
141187
mProgressBar.setVisibility(View.GONE);
142188
ToastUtils.show("下载出错:" + e.getMessage());
143189
}
@@ -148,4 +194,64 @@ public void onDownloadError(DownloadTask task, Exception e) {
148194
break;
149195
}
150196
}
197+
198+
/**
199+
* 安装 Apk
200+
*/
201+
private void installApk(final Context context, final File file) {
202+
XXPermissions.with(MainActivity.this)
203+
// 安装包权限
204+
.permission(Permission.REQUEST_INSTALL_PACKAGES)
205+
.request(new OnPermission() {
206+
@Override
207+
public void hasPermission(List<String> granted, boolean isAll) {
208+
if (isAll) {
209+
Intent intent = new Intent();
210+
intent.setAction(Intent.ACTION_VIEW);
211+
Uri uri;
212+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
213+
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
214+
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
215+
} else {
216+
uri = Uri.fromFile(file);
217+
}
218+
219+
intent.setDataAndType(uri, "application/vnd.android.package-archive");
220+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
221+
context.startActivity(intent);
222+
}
223+
}
224+
225+
@Override
226+
public void noPermission(List<String> denied, boolean quick) {
227+
228+
}
229+
});
230+
}
231+
232+
/**
233+
* 将 Drawable 写入到文件中
234+
*/
235+
private void drawableToFile(Drawable drawable, File file) {
236+
if (drawable == null) {
237+
return;
238+
}
239+
240+
try {
241+
if (file.exists()) {
242+
file.delete();
243+
}
244+
245+
if (!file.exists()) {
246+
file.createNewFile();
247+
}
248+
249+
FileOutputStream out;
250+
out = new FileOutputStream(file);
251+
((BitmapDrawable) drawable).getBitmap().compress(Bitmap.CompressFormat.PNG, 100, out);
252+
out.close();
253+
} catch (IOException e) {
254+
e.printStackTrace();
255+
}
256+
}
151257
}

app/src/main/java/com/hjq/http/demo/MyApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public void onCreate() {
3333
.setServer(server)
3434
// 设置请求处理策略
3535
.setHandler(new RequestHandler())
36+
// 设置请求重试次数
37+
.setRetryCount(3)
3638
// 添加全局请求参数
3739
//.addParam("token", "6666666")
3840
// 添加全局请求头

0 commit comments

Comments
 (0)