1.方便的网络请求
api 'com.zhy:okhttputils:2.6.2'
正常请求:
OkHttpUtils
.get()
.url(BuildConfig.BASE_URL + "appVersion/getLast/android")
.addHeader("Authorization", AccountManager.getToken())
.build()
.execute(new StringCallback() {
@Override
public void onError(Call call, Exception e, int id) {
KLog.d("chenshichun" + e.getMessage());
}
@Override
public void onResponse(String response, int id) {
Log.d("chenshichun", "onResponse: " + response);
UpdateBean updateBean = JsonUtils.jsonToBean(response, UpdateBean.class);
if (updateBean.getData() != null && updateBean.getData().getVersionCode() > APKVersionCodeUtils.getVersionCode(MainActivity.this)) {
UpdateDialog updateDialog = new UpdateDialog(MainActivity.this, updateBean);
updateDialog.setOnClickListener(new UpdateDialog.OnClickListener() {
@Override
public void update() {
downloadApp(updateBean);
ToastUtil.show("开始下载");
}
});
updateDialog.show();
}
}
});
下载文件请求:
OkHttpUtils.
get()
.url(updateBean.getData().getDownloadUrl())
.build()
.execute(new FileCallBack(Environment.getExternalStorageDirectory().getAbsolutePath(), "dlb.apk") {
@Override
public void inProgress(float progress, long total, int id) {
super.inProgress(progress, total, id);
}
@Override
public void onError(Call call, Exception e, int id) {
ToastUtil.show("下载失败");
}
@Override
public void onResponse(File response, int id) {
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".fileProvider", response);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(response), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
startActivity(intent);
}
});
2.屏幕适配
api 'com.zhy:autolayout:1.4.5'