Android使用Xutil3.0下载文件.md

与天地兮比寿,与日月兮齐光。这篇文章主要讲述Android使用Xutil3.0下载文件.md相关的知识,希望能为你提供帮助。
android使用Xutil3.0下载文件.md 新建项目:【Android使用Xutil3.0下载文件.md】引入依赖:

implementation ' org.xutils:xutils:3.5.0'

添加权限:
< uses-permission android:name=" android.permission.INTERNET" /> < uses-permission android:name=" android.permission.WRITE_EXTERNAL_STORAGE" />

修改文件:activity_main.xml
< ?xml version=" 1.0" encoding=" utf-8" ?> < LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android" xmlns:app=" http://schemas.android.com/apk/res-auto" xmlns:tools=" http://schemas.android.com/tools" android:layout_width=" match_parent" android:layout_height=" match_parent" android:orientation=" vertical" tools:context=" .MainActivity" > < TextView android:layout_width=" wrap_content" android:layout_height=" wrap_content" android:text=" Hello World!" app:layout_constraintBottom_toBottomOf=" parent" app:layout_constraintLeft_toLeftOf=" parent" app:layout_constraintRight_toRightOf=" parent" app:layout_constraintTop_toTopOf=" parent" /> < Button android:id=" @+id/xiazai" android:layout_width=" wrap_content" android:layout_height=" wrap_content" android:text=" 下载测试按钮" /> < ProgressBar android:id=" @+id/prg" android:layout_width=" match_parent" android:layout_height=" wrap_content" style=" @style/Base.Widget.AppCompat.ProgressBar.Horizontal" /> < /LinearLayout>

MainActivity:
package com.example.administrator.xutilstest; import android.media.SoundPool; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.Toast; import org.xutils.common.Callback; import org.xutils.ex.HttpException; import org.xutils.http.RequestParams; import org.xutils.x; import java.io.File; public class MainActivity extends AppCompatActivity {private ProgressBar progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressBar= findViewById(R.id.prg); x.Ext.init(getApplication()); x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能.Button button = findViewById(R.id.xiazai); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String url = " http://localhost:8080/gh.zip" ; RequestParams params = new RequestParams(url); // 默认缓存存活时间, 单位:毫秒(如果服务器没有返回有效的max-age或Expires则参考) params.setCacheMaxAge(1000 * 60); x.http().get(params, new Callback.ProgressCallback< File> () { @Override public void onSuccess(File result) { System.out.println(" 下载成功" ); }@Override public void onError(Throwable ex, boolean isOnCallback) {}@Override public void onCancelled(CancelledException cex) {}@Override public void onFinished() {}private boolean hasError = false; private String result = null; @Override public void onWaiting() {}@Override public void onStarted() {System.out.println(" 开始下载" ); }@Override public void onLoading(long total, long current, boolean isDownloading) { progressBar.setMax((int) total); progressBar.setProgress((int) current); }}); } }); } }


    推荐阅读