UpdateAppHttpUtils

古之立大事者,不惟有超世之才,亦必有坚忍不拔之志。这篇文章主要讲述UpdateAppHttpUtils相关的知识,希望能为你提供帮助。
鏍囩锛?a href='http://www.mamicode.com/so/1/activex' title='activex'>activex      params      sub      for      dad      eth      use      ORC      version     
鍩轰簬绗笁鏂规鏋秛pdate-app,杩欎釜宸ュ叿绫绘湁寰呭畬鍠?杩欓噷鍙槸淇濆瓨涓€涓?/p>

compile ' com.qianwen:update-app:3.5.2'

import android.app.Activity; import android.support.annotation.NonNull; import android.text.TextUtils; import com.vector.update_app.HttpManager; import com.vector.update_app.UpdateAppBean; import com.vector.update_app.UpdateAppManager; import com.vector.update_app.UpdateCallback; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import cn.dongchen.bigsteward.library_common.R; import cn.dongchen.bigsteward.library_common.gson.VersionGson; import cn.dongchen.bigsteward.library_common.network.RetrofitUtils; import cn.dongchen.bigsteward.library_common.network.result.BaseData; import cn.dongchen.bigsteward.library_common.permission.PermissionCallBack; import cn.dongchen.bigsteward.library_common.permission.PermissionsUtils; import cn.dongchen.bigsteward.library_common.rx.BaseObserver; import cn.dongchen.bigsteward.library_common.rx.BaseRxJava; import cn.dongchen.bigsteward.library_common.toast.ToastUtils; import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.functions.Consumer; import io.reactivex.schedulers.Schedulers; /** * @author fada *涓嬭浇宸ュ叿绫? */public class UpdateAppHttpUtil implements HttpManager { /** * 鏇存柊鐗堟湰 * * @param activity */ public static void updateVersion(Activity activity, boolean needToast) { new PermissionsUtils .Build() .permissions(PermissionsUtils.PERMISSION_WRITE_EXTERNAL_STORAGE) .message(" 鍏佽搴旂敤绋嬪簭鍐欏叆澶栭儴瀛樺偍" ) .permissionCallBack(new PermissionCallBack() { @Override public void granted() { update(activity, needToast); }@Override public void refuse() { ToastUtils.getInstance().showText(" 鎮ㄦ嫆缁濅簡鏉冮檺,鏃犳硶缁х画鏇存柊" ); }@Override public void noAgain() { ToastUtils.getInstance().showText(" 鎮ㄦ嫆缁濅簡鏉冮檺,鏃犳硶缁х画鏇存柊" ); } }) .build() .getPermission(activity); }/** * 鏇存柊app * * @param activity * @param needToast */ private static void update(Activity activity, boolean needToast) { Map< String, String> params = new HashMap< > (2); params.put(" versionNum" , SystemUtils.getVersionName(activity)); params.put(" type" , " 0" ); new UpdateAppManager .Builder() //褰撳墠Activity .setActivity(activity) //鏇存柊鍦板潃 .setUpdateUrl(" http://122.225.55.70:9880/Butler/checkVersionApp" ) .setParams(params) //瀹炵幇httpManager鎺ュ彛鐨勫璞?.setHttpManager(new UpdateAppHttpUtil()) //璁剧疆涓讳綋鑹?.setThemeColor(activity.getResources().getColor(R.color.colorAccent)) //璁剧疆澶村浘鐗?.setTopPic(R.drawable.update_dada).build() .checkNewApp(new UpdateCallback() { @Override protected UpdateAppBean parseJson(String json) { UpdateAppBean updateAppBean = new UpdateAppBean(); if (TextUtils.isEmpty(json)) { updateAppBean.setUpdate(" No" ); if (needToast) { ToastUtils.getInstance().showText(" 宸茬粡鏄渶鏂扮増浜?#34; ); } return updateAppBean; } VersionGson version = GsonUtils.getInstance().fromJson(json, VersionGson.class); //鏄惁闇€瑕佹洿鏂?updateAppBean.setUpdate(" Yes" ); //鏂囦欢鍦板潃 updateAppBean.setApkFileUrl(version.getUploadUrl()); //鏂扮増鏈彿 updateAppBean.setNewVersion(version.getVersionNum()); //寮哄埗鏇存柊 updateAppBean.setConstraint(version.getNeedForceUpdate() == 0); //璁剧疆鍐呭 updateAppBean.setUpdateLog(version.getUpdateContent()); return updateAppBean; }@Override protected void hasNewApp(UpdateAppBean updateApp, UpdateAppManager updateAppManager) { super.hasNewApp(updateApp, updateAppManager); }@Override protected void onAfter() { super.onAfter(); }@Override protected void noNewApp(String error) { super.noNewApp(error); }@Override protected void onBefore() { super.onBefore(); } }); }/** * 寮傛get * * @param urlget璇锋眰鍦板潃 * @param paramsget鍙傛暟 * @param callBack 鍥炶皟 */ @Override public void asyncGet(@NonNull String url, @NonNull Map< String, String> params, @NonNull final Callback callBack) { Observable< BaseData< VersionGson> > observable = RetrofitUtils .getInstance() .getServerices() .checkVersion(params); BaseRxJava.bindRx(observable, new BaseObserver< BaseData< VersionGson> > () { @Override public void onError(String error) { callBack.onError(error); }@Override public void onNext(BaseData< VersionGson> versionGsonBaseData) { VersionGson data = https://www.songbingjia.com/android/versionGsonBaseData.getData(); if (data != null & & versionGsonBaseData.getCode() != 0) { callBack.onResponse(data.toString()); } else { callBack.onResponse(" " ); } }@Override public void onComplete() {} }); }/** * 寮傛post * * @param urlpost璇锋眰鍦板潃 * @param paramspost璇锋眰鍙傛暟 * @param callBack 鍥炶皟 */ @Override public void asyncPost(@NonNull String url, @NonNull Map< String, String> params, @NonNull final Callback callBack) { }/** * 涓嬭浇 * * @param url涓嬭浇鍦板潃 * @param path鏂囦欢淇濆瓨璺緞 * @param fileName 鏂囦欢鍚嶇О * @param callback 鍥炶皟 */ @Override public void download(@NonNull String url, @NonNull String path, @NonNull String fileName, @NonNull final FileCallback callback) { LogUtils.d(" download() called with: url = [" + url + " ], path = [" + path + " ], fileName " + " = [" + fileName + " ], callback = [" + callback + " ]" ); callback.onBefore(); RetrofitUtils .getInstance() .getServerices() .downloadFile(url) .subscribeOn(Schedulers.io()) .map(responseBody -> { File file = new File(path); //鐩綍鏄惁瀛樺湪,涓嶅瓨鍦ㄥ垯鍒涘缓 if (!file.exists()) { file.mkdir(); } //鍒涘缓鏂囦欢 file = new File(file, fileName); //鏂囦欢璇诲啓 byte[] bytes = new byte[1024 * 1024]; int len = 0; final long total = responseBody.contentLength(); long sum = 0; FileOutputStream outputStream = new FileOutputStream(file); InputStream inputStream = responseBody.byteStream(); while ((len = inputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, len); outputStream.flush(); sum += len; final float progress = sum * 1.0f / total; RetrofitUtils.getInstance().getRetrofit().callbackExecutor().execute(() -> { callback.onProgress(progress, total); }); }//鍏抽棴娴?outputStream.close(); inputStream.close(); return file; }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new BaseObserver< File> () { @Override public void onError(String error) { callback.onError(error); }@Override public void onNext(File file) { callback.onResponse(file); }@Override public void onComplete() { } }); } }

【UpdateAppHttpUtils】 鍘熸枃:澶т笓鏍?/a>   UpdateAppHttpUtils



    推荐阅读