An example of Android using xutils3 for breakpoint Download

Time:2021-1-17

Tools: Android studio 3.0

usage method:

1: In build.gradle ( Module:app )Add dependency to


implementaion 'org.xutils:xutils:3.5.1'

2: Create a myapp class to initialize xutils. The class name can be customized


public class MyApp extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    x.Ext.init(this);
  }
}

3: Add permissions to Android manifest and register myapp

//Add permissions
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/>
<uses-permission android:name=”android.permission.INTERNET”/>
//Sign up for myapp

Add in < Application > node android:name= “.MyApp’

4: Using the download method

public void download(View view){
    String path=et.getText().toString().trim();
    RequestParams params=new RequestParams(path);
    params.setSaveFilePath(Environment.getExternalStorageDirectory()+"/myapp/");
    params.setAutoRename(true);
    x.http().post(params,new Callback.ProgressCallback<File>(){
      @Override
      public void onWaiting() {
 
      }
      @Override
      public void onStarted() {
 
      }
      @Override
      public void onLoading(long total, long current, boolean isDownloading) {
        pd.setMax((int)total);
        pd.setProgress((int)current);
      }
      @Override
      public void onSuccess(File result) {
        Log. I ("test", "download complete");
      }
      @Override
      public void onError(Throwable ex, boolean isOnCallback) {
 
      }
      @Override
      public void onCancelled(CancelledException cex) {
 
      }
      @Override
      public void onFinished() {
 
      }
    });
  }

Knowledge point expansion:

About xutils3

Xutils includes ORM, HTTP (s), image and view annotations, but it is still very lightweight (246k), and has powerful features and easy to expand

  • The cornerstone of stability: abstask and unified callback interface callback. Any exception, even if your callback method has an exception, will enter onerror. In any case, onfinished will always let you know that the task is over
  • Based on the efficient and stable ORM tool, HTTP module can more easily implement cookie (support domain, path, expiration and other features) and cache (support cache control, last modified, Etag and other features)
  • With the powerful support of HTTP and its download cache, the implementation of the image module is quite simple, and supports the recycling of images held by view but removed by MEM cache to reduce the flicker of page fallback
  • The view annotation module only has more than 400 lines of code, but it flexibly supports all kinds of view injection and event binding, including the support of listener with more methods