xUtils3下载文件

1.import  xUtils3工程

    修改Project的.gradle文件,(其实就是把xutils3下面的抄过来,要记得改版本,原来是1.3不行,改到最新的就好了)

dependencies {
    classpath 'com.android.tools.build:gradle:2.2.3'
    //添加下面的2行
    classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
    classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
2.添加进 Module 的依赖

3.开始正题吧

    3.1 权限


   3.2 初始化,这个类要在AndroidManifest.xml文件节点下配置

    public class BaseApplication extends Application {  
      
        @Override  
        public void onCreate() {  
            super.onCreate();  
            x.Ext.init(this);  
        }  
    }  
    3.3 下载实现

String url = "http://www.ooxx.com/xxx.apk";
RequestParams requestParams = new RequestParams(url);
//String savePath = Environment.getExternalStorageDirectory().getPath()+ File.separator+"mobilesafe.apk";
//getExternalFilesDir SDCard/Android/data/你的应用的包名/files/ 目录,一般用来存放长时间保存的数据

String savePath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getPath()+ File.separator+"mobilesafe.apk";
requestParams.setSaveFilePath(savePath);
Log.d(tag,url+"\n"+savePath);
org.xutils.x.http().get(requestParams, new Callback.CommonCallback() {
      @Override
      public void onSuccess(File result) {
          Log.d(tag,"onSuccess");
      }

      @Override
      public void onError(Throwable ex, boolean isOnCallback) {
          Log.d(tag,"onError");
      }

      @Override
      public void onCancelled(CancelledException cex) {
          Log.d(tag,"onCancelled");
      }

@Override public void onFinished() { Log.d(tag,"onFinished"); } });

Callback.CommonCallback 代码自动补全写的是Object类型,注意下


你可能感兴趣的:(学习路上)