Skip to content

Commit

Permalink
1,去掉对fastjson的依赖
Browse files Browse the repository at this point in the history
2,简化json协议
3,新增自定义接口协议
  • Loading branch information
WVector committed Jul 5, 2017
1 parent 6e878ea commit baefffd
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 76 deletions.
96 changes: 88 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
* [功能介绍](#功能介绍)
* [效果图与示例 apk](#效果图与示例-apk)
* [使用](#使用)
* [自定义接口协议](#自定义接口协议)
* [License](#license)

## 功能介绍

- [x] 实现app版本更新
- [x] 自定义接口协议,可以不改变现有项目的协议就能使用
- [x] 支持get,post请求
- [x] 支持进度显示,对话框进度条,和通知栏进度条展示
- [x] 支持后台下载
Expand Down Expand Up @@ -38,7 +40,7 @@

```gradle
dependencies {
compile 'com.qianwen:update-app:3.0.0'
compile 'com.qianwen:update-app:3.1.0'
}
```

Expand Down Expand Up @@ -80,10 +82,7 @@ dependencies {
"apk_file_url": "https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/apk/app-debug.apk", //apk下载地址
"update_log": "1,添加删除信用卡接口\r\n2,添加vip认证\r\n3,区分自定义消费,一个小时不限制。\r\n4,添加放弃任务接口,小时内不生成。\r\n5,消费任务手动生成。",//更新内容
"target_size": "5M",//apk大小
"constraint": false,//是否强制更新
"status": "success",
"msg": "ok",
"timestamp": 1498785412690
"constraint": false//是否强制更新
}

```
Expand All @@ -94,9 +93,6 @@ dependencies {

{
"update": "No",//没有新版本
"status": "success",
"msg": "ok",
"timestamp": 1498785412690
}

```
Expand Down Expand Up @@ -205,6 +201,7 @@ dependencies {

### 3,客户端检测是否有新版本,并且更新下载


```java

String updateUrl = "https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/json/json.txt";
Expand Down Expand Up @@ -257,6 +254,89 @@ dependencies {


```

## 自定义接口协议

自定义接口协议,根据自己项目的接口,自己实现 parseJson 方法

```java

new UpdateAppManager
.Builder()
//当前Activity
.setActivity(this)
//实现httpManager接口的对象
.setHttpManager(new UpdateAppHttpUtil())
//更新地址
.setUpdateUrl("https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/json/json.txt")
.build()
//检测是否有新版本
.checkNewApp(new UpdateCallback() {
/**
* 解析json,自定义协议
*
* @param json 服务器返回的json
* @return UpdateAppBean
*/
@Override
protected UpdateAppBean parseJson(String json) {
UpdateAppBean updateAppBean = new UpdateAppBean();
try {
JSONObject jsonObject = new JSONObject(json);
updateAppBean
//是否更新Yes,No
.setUpdate(jsonObject.getString("update"))
//新版本号
.setNew_version(jsonObject.getString("new_version"))
//下载地址
.setApk_file_url(jsonObject.getString("apk_file_url"))
//大小
.setTarget_size(jsonObject.getString("target_size"))
//更新内容
.setUpdate_log(jsonObject.getString("update_log"))
//是否强制更新
.setConstraint(jsonObject.getBoolean("constraint"));
} catch (JSONException e) {
e.printStackTrace();
}
return updateAppBean;
}
/**
* 有新版本
*
* @param updateApp 新版本信息
* @param updateAppManager app更新管理器
*/
@Override
public void hasNewApp(UpdateAppBean updateApp, UpdateAppManager updateAppManager) {
updateAppManager.showDialog();
}
/**
* 网络请求之前
*/
@Override
public void onBefore() {
CProgressDialogUtils.showProgressDialog(MainActivity.this);
}
/**
* 网路请求之后
*/
@Override
public void onAfter() {
CProgressDialogUtils.cancelProgressDialog(MainActivity.this);
}
/**
* 没有新版本
*/
@Override
public void noNewApp() {
Toast.makeText(MainActivity.this, "没有新版本", Toast.LENGTH_SHORT).show();
}
});

```


#### 进度条使用的是代码家的「[NumberProgressBar](https://github.com/daimajia/NumberProgressBar)

## License
Expand Down
85 changes: 85 additions & 0 deletions app/src/main/java/com/vector/appupdatedemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import com.vector.update_app.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import rx.functions.Action1;

import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
Expand All @@ -39,6 +42,7 @@ protected void onCreate(Bundle savedInstanceState) {
.timeout(20 * 1000);

DrawableUtil.setTextStrokeTheme((Button) findViewById(R.id.btn_get_permission));
DrawableUtil.setTextStrokeTheme((Button) findViewById(R.id.btn_update_app_4));
DrawableUtil.setTextStrokeTheme((Button) findViewById(R.id.btn_update_app), color1);
DrawableUtil.setTextStrokeTheme((Button) findViewById(R.id.btn_update_app_2), color2);
DrawableUtil.setTextStrokeTheme((Button) findViewById(R.id.btn_update_app_3), color3);
Expand All @@ -47,6 +51,7 @@ protected void onCreate(Bundle savedInstanceState) {
ImageView im = (ImageView) findViewById(R.id.iv);

im.setImageBitmap(Utils.drawableToBitmap(Utils.getAppIcon(this)));

}

public void updateApp(View view) {
Expand Down Expand Up @@ -193,4 +198,84 @@ public void call(Boolean aBoolean) {
public void test(View view) {
startActivity(new Intent(this, DialogActivity.class));
}

public void updateApp4(View view) {
new UpdateAppManager
.Builder()
//当前Activity
.setActivity(this)
//实现httpManager接口的对象
.setHttpManager(new UpdateAppHttpUtil())
//更新地址
.setUpdateUrl("https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/json/json.txt")
.build()
//检测是否有新版本
.checkNewApp(new UpdateCallback() {
/**
* 解析json,自定义协议
*
* @param json 服务器返回的json
* @return UpdateAppBean
*/
@Override
protected UpdateAppBean parseJson(String json) {
UpdateAppBean updateAppBean = new UpdateAppBean();
try {
JSONObject jsonObject = new JSONObject(json);
updateAppBean
//是否更新Yes,No
.setUpdate(jsonObject.getString("update"))
//新版本号
.setNew_version(jsonObject.getString("new_version"))
//下载地址
.setApk_file_url(jsonObject.getString("apk_file_url"))
//大小
.setTarget_size(jsonObject.getString("target_size"))
//更新内容
.setUpdate_log(jsonObject.getString("update_log"))
//是否强制更新
.setConstraint(jsonObject.getBoolean("constraint"));
} catch (JSONException e) {
e.printStackTrace();
}
return updateAppBean;
}

/**
* 有新版本
*
* @param updateApp 新版本信息
* @param updateAppManager app更新管理器
*/
@Override
public void hasNewApp(UpdateAppBean updateApp, UpdateAppManager updateAppManager) {
updateAppManager.showDialog();
}

/**
* 网络请求之前
*/
@Override
public void onBefore() {
CProgressDialogUtils.showProgressDialog(MainActivity.this);
}

/**
* 网路请求之后
*/
@Override
public void onAfter() {
CProgressDialogUtils.cancelProgressDialog(MainActivity.this);
}

/**
* 没有新版本
*/
@Override
public void noNewApp() {
Toast.makeText(MainActivity.this, "没有新版本", Toast.LENGTH_SHORT).show();
}
});

}
}
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
android:text="更新App(颜色39c1e9)"
/>

<Button
android:id="@+id/btn_update_app_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:onClick="updateApp4"
android:text="自定义接口协议"
/>

<Button
android:id="@+id/btn_test"
Expand Down
5 changes: 1 addition & 4 deletions json/json.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@
"apk_file_url": "https://raw.githubusercontent.com/WVector/AppUpdateDemo/master/apk/app-debug.apk",
"update_log": "1,添加删除信用卡接口\r\n2,添加vip认证\r\n3,区分自定义消费,一个小时不限制。\r\n4,添加放弃任务接口,小时内不生成。\r\n5,消费任务手动生成。",
"target_size": "5M",
"constraint": false,
"status": "success",
"msg": "ok",
"timestamp": 1498785412690
"constraint": false
}
3 changes: 1 addition & 2 deletions update-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.alibaba:fastjson:1.1.58.android'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
Expand All @@ -42,7 +41,7 @@ publish {
userOrg = 'qianwen'//bintray.com用户名
groupId = 'com.qianwen'//jcenter上的路径
artifactId = 'update-app'//项目名称
publishVersion = '3.0.0'//版本号
publishVersion = '3.1.0'//版本号
desc = 'App update tools'
website = 'https://github.com/WVector/AppUpdateDemo'
}
Expand Down
Loading

0 comments on commit baefffd

Please sign in to comment.