Skip to content

Commit

Permalink
新增上传文件请求示例
Browse files Browse the repository at this point in the history
新增支持请求失败重试机制
修复极速下载逻辑的 Bug
优化 Http 处理器参数对象
优化表单提交和文件提交的逻辑
  • Loading branch information
880634 committed Dec 17, 2019
1 parent 5a456f5 commit 28668d4
Show file tree
Hide file tree
Showing 38 changed files with 941 additions and 512 deletions.
Binary file modified EasyHttp.apk
Binary file not shown.
Binary file modified EasyHttp.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 5 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#### Gradle 集成

dependencies {
implementation 'com.hjq:http:2.0'
implementation 'com.hjq:http:3.0'
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
implementation 'com.google.code.gson:gson:2.8.5'
}
Expand Down Expand Up @@ -51,6 +51,8 @@
.setServer(server)
// 设置请求处理策略
.setHandler(new RequestHandler())
// 设置请求重试次数
.setRetryCount(3)
// 添加全局请求参数
//.addParam("token", "6666666")
// 添加全局请求头
Expand Down Expand Up @@ -97,24 +99,7 @@

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

#### Get 请求

EasyHttp.get(this)
.api(new LoginApi()
.setUserName("Android 轮子哥")
.setPassword("123456"))
.request(new OnHttpListener<HttpData<LoginBean>>() {

@Override
public void onSucceed(HttpData<LoginBean> data) {
ToastUtils.show("登录成功");
}

@Override
public void onFail(Exception e) {}
});

#### Post 请求
#### 发起请求

EasyHttp.post(this)
.api(new LoginApi()
Expand Down Expand Up @@ -235,7 +220,7 @@

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

#### 特别鸣谢
#### 特别感谢

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

Expand Down
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.hjq.http.demo"
minSdkVersion 14
targetSdkVersion 28
versionCode 20
versionName "2.0"
versionCode 30
versionName "3.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down Expand Up @@ -43,5 +43,5 @@ dependencies {
implementation 'com.squareup.okhttp3:okhttp:3.12.1'

// 内存泄漏捕捉:https://github.com/square/leakcanary
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0-beta-3'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0'
}
15 changes: 15 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- 安装包的权限 -->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

<application
android:name=".MyApplication"
android:icon="@mipmap/ic_launcher"
Expand All @@ -21,6 +24,18 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/AppTheme"
tools:targetApi="n">

<!-- 适配 Android 7.0 文件意图 -->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
142 changes: 124 additions & 18 deletions app/src/main/java/com/hjq/http/demo/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
package com.hjq.http.demo;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.view.View;
import android.widget.ProgressBar;

import com.hjq.http.EasyHttp;
import com.hjq.http.demo.http.model.HttpData;
import com.hjq.http.demo.http.request.SearchAuthorApi;
import com.hjq.http.demo.http.request.SearchBlogsApi;
import com.hjq.http.demo.http.request.UpdateImageApi;
import com.hjq.http.demo.http.response.SearchBean;
import com.hjq.http.listener.OnDownloadListener;
import com.hjq.http.listener.OnHttpListener;
import com.hjq.http.model.DownloadTask;
import com.hjq.http.model.DownloadInfo;
import com.hjq.http.model.HttpMethod;
import com.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.hjq.toast.ToastUtils;

import org.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;


/**
* author : Android 轮子哥
* github : https://github.com/getActivity/EasyHttp
* time : 2019/05/19
* desc : 网络请求示例
*/
public class MainActivity extends BaseActivity implements View.OnClickListener, OnPermission {

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

findViewById(R.id.btn_main_get).setOnClickListener(this);
findViewById(R.id.btn_main_post).setOnClickListener(this);
findViewById(R.id.btn_main_update).setOnClickListener(this);
findViewById(R.id.btn_main_download).setOnClickListener(this);

requestPermission();
Expand Down Expand Up @@ -72,7 +92,7 @@ protected void onRestart() {
super.onRestart();
if (XXPermissions.isHasPermission(this, Permission.Group.STORAGE)) {
hasPermission(null, true);
}else {
} else {
requestPermission();
}
}
Expand All @@ -87,12 +107,14 @@ public void onClick(View v) {
.request(new OnHttpListener<HttpData<SearchBean>>() {

@Override
public void onSucceed(HttpData<SearchBean> data) {
public void onSucceed(HttpData<SearchBean> result) {
ToastUtils.show("请求成功");
}

@Override
public void onFail(Exception e) {}
public void onFail(Exception e) {
ToastUtils.show(e.getMessage());
}
});
break;
case R.id.btn_main_post:
Expand All @@ -102,42 +124,66 @@ public void onFail(Exception e) {}
.request(new OnHttpListener<HttpData<SearchBean>>() {

@Override
public void onSucceed(HttpData<SearchBean> data) {
public void onSucceed(HttpData<SearchBean> result) {
ToastUtils.show("请求成功");
}

@Override
public void onFail(Exception e) {}
public void onFail(Exception e) {
ToastUtils.show(e.getMessage());
}
});
break;
case R.id.btn_main_update:
File file = new File(Environment.getExternalStorageDirectory(), getString(R.string.app_name) + ".png");
// 生成图片到本地
drawableToFile(ContextCompat.getDrawable(this, R.mipmap.ic_launcher), file);

EasyHttp.post(this)
.api(new UpdateImageApi()
.setImage(file))
.request(new OnHttpListener<JSONObject>() {

@Override
public void onSucceed(JSONObject result) {
ToastUtils.show("上传成功");
}

@Override
public void onFail(Exception e) {
ToastUtils.show(e.getMessage());
}
});
break;
case R.id.btn_main_download:
EasyHttp.download(this)
.method(HttpMethod.GET)
.file(new File(Environment.getExternalStorageDirectory(), "手机QQ.apk"))
.url("https://qd.myapp.com/myapp/qqteam/AndroidQQ/mobileqq_android.apk")
.md5("47CBDF2A2940B7773DD1B63CBCFD86E1")
//.url("http://dldir1.qq.com/weixin/android/weixin708android1540.apk")
.file(new File(Environment.getExternalStorageDirectory(), "微信.apk"))
//.url("https://qd.myapp.com/myapp/qqteam/AndroidQQ/mobileqq_android.apk")
.url("http://dldir1.qq.com/weixin/android/weixin708android1540.apk")
.md5("2E8BDD7686474A7BC4A51ADC3667CABF")
.listener(new OnDownloadListener() {

@Override
public void onDownloadStart(DownloadTask task) {
public void onDownloadStart(DownloadInfo info) {
mProgressBar.setVisibility(View.VISIBLE);
ToastUtils.show("下载开始:" + task.getFile().getName());
ToastUtils.show("下载开始:" + info.getFile().getName());
}

@Override
public void onDownloadProgress(DownloadTask task) {
mProgressBar.setProgress(task.getProgress());
public void onDownloadProgress(DownloadInfo info) {
mProgressBar.setProgress(info.getDownloadProgress());
}

@Override
public void onDownloadComplete(DownloadTask task) {
public void onDownloadComplete(DownloadInfo info) {
mProgressBar.setVisibility(View.GONE);
ToastUtils.show("下载完成:" + task.getFile().getPath());
ToastUtils.show("下载完成:" + info.getFile().getPath());
installApk(MainActivity.this, info.getFile());
}

@Override
public void onDownloadError(DownloadTask task, Exception e) {
public void onDownloadError(DownloadInfo info, Exception e) {
mProgressBar.setVisibility(View.GONE);
ToastUtils.show("下载出错:" + e.getMessage());
}
Expand All @@ -148,4 +194,64 @@ public void onDownloadError(DownloadTask task, Exception e) {
break;
}
}

/**
* 安装 Apk
*/
private void installApk(final Context context, final File file) {
XXPermissions.with(MainActivity.this)
// 安装包权限
.permission(Permission.REQUEST_INSTALL_PACKAGES)
.request(new OnPermission() {
@Override
public void hasPermission(List<String> granted, boolean isAll) {
if (isAll) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} else {
uri = Uri.fromFile(file);
}

intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}

@Override
public void noPermission(List<String> denied, boolean quick) {

}
});
}

/**
* 将 Drawable 写入到文件中
*/
private void drawableToFile(Drawable drawable, File file) {
if (drawable == null) {
return;
}

try {
if (file.exists()) {
file.delete();
}

if (!file.exists()) {
file.createNewFile();
}

FileOutputStream out;
out = new FileOutputStream(file);
((BitmapDrawable) drawable).getBitmap().compress(Bitmap.CompressFormat.PNG, 100, out);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2 changes: 2 additions & 0 deletions app/src/main/java/com/hjq/http/demo/MyApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public void onCreate() {
.setServer(server)
// 设置请求处理策略
.setHandler(new RequestHandler())
// 设置请求重试次数
.setRetryCount(3)
// 添加全局请求参数
//.addParam("token", "6666666")
// 添加全局请求头
Expand Down
Loading

0 comments on commit 28668d4

Please sign in to comment.