android_37_使用开源xUtils进行下载

 

将Jar包copy到libs目录下

 

效果:

 

 

 

 

 

清单:

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sg31.xutils"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    // 注意权限 
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

 

 

 

 

 

 

布局:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.sg31.xutils.MainActivity" 
    android:orientation="vertical"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下载" 
        android:onClick="downloadBtnClicked"
        />
    <TextView
        android:id="@+id/tv_failure"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
    <ProgressBar
        android:id="@+id/pb" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar.Horizontal"
        />
    <TextView
        android:id="@+id/tv_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

</LinearLayout>

 

 

 

 

 

 

 

代码:

 

package com.sg31.xutils;

import java.io.File;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

	private TextView tv_failure;
    private TextView tv_progress;
    private ProgressBar pb;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        tv_failure = (TextView) findViewById(R.id.tv_failure);
        tv_progress = (TextView) findViewById(R.id.tv_progress);
        pb = (ProgressBar) findViewById(R.id.pb);
        
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    
    public void downloadBtnClicked(View v){
        HttpUtils utils = new HttpUtils();
        
        String fileName = "NEXT_TO_YOU.mp3";  
        // 确定下载地址  
        String path = "http://od8vj43og.bkt.clouddn.com/" + fileName;  
        utils.download(path, //下载地址
                       "sdcard/"+fileName, //文件保存路径
                       true,//是否支持断点续传
                       true, new RequestCallBack<File>() {
                           
                           //下载成功后调用
                           @Override
                           public void onSuccess(ResponseInfo<File> arg0) {
                               Toast.makeText(MainActivity.this, arg0.result.getPath(), 0).show();
                           }
                           
                           //下载失败调用
                           @Override
                           public void onFailure(HttpException arg0, String arg1) {
                               tv_failure.setText(arg1);
                           }
                           
                           @Override
                           public void onLoading(long total, long current,
                                                 boolean isUploading) {
                               super.onLoading(total, current, isUploading);
                               pb.setMax((int)total);
                               pb.setProgress((int)current);
                               tv_progress.setText(current * 100 / total + "%");
                           }
						 
                       });
    }
    
}

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一个Android基于快速开发的一个框架 xUtils 它是在aFinal基础上进行重构和扩展的框架 相比aFinal有很大的改善 同时 如果如果你的应用是基于网络的 那么只要处理得当 它会让你彻底的摆脱各种工具类和重复代码的困扰 xUtils 包含了很多实用的android工具 xUtils 源于Afinal框架 对Afinal进行了大量重构 使得xUtils支持大文件上传 更全面的http请求协议支持 拥有更加灵活的ORM 更多的事件注解支持且不受混淆影响 xUtils的四大组件: 一 ViewUtils 你受够了重复冗长的findViewById了嘛 你受够了各种监听事件的绑定了嘛 在这里 你只需要一句注解 如@ViewInject @OnClick 就能轻松摆脱小白似的代码 大大的上了一个档次 二 HttpUtils 支持的HTTP七种请求方式 非常便捷的满足你的接口请求的需要 同时还支持大文件上传下载 以及同步异步请求 三 BitmapUtils 你的程序因OOM强制关闭过嘛 你在为加在网络图片头疼嘛 有了组件 你将永久摆脱前面的问题 四 DbUtils 简单易用又出色的ORM框架 真的是谁用谁知道 直接轻松存储各种对象到sqlite数据库中 同时也能非常方便的进行各种条件查询 甚至分页查询 还有对表中数据的更新删除等操作 真正的实现 一行代码就可以进行增删改查 并且可通过注解自定义表名 列名 外键 唯一性约束 NOT NULL约束 CHECK约束等 支持事务 摘自github ">一个Android基于快速开发的一个框架 xUtils 它是在aFinal基础上进行重构和扩展的框架 相比aFinal有很大的改善 同时 如果如果你的应用是基于网络的 那么只要处理得当 它会让你彻底的摆脱各种工具类和重复代码的困扰 [更多]
Android xUtils3 是一个非常方便的 Android 开发工具包,其中包含了许多方便实用的功能,例如网络请求、图片加载等。下面是使用 xUtils3 获取网络图片的示例代码: 1. 在 build.gradle 中添加 xUtils3 的依赖: ```groovy dependencies { implementation 'org.xutils:xutils:3.5.0' } ``` 2. 在 AndroidManifest.xml 中添加网络权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> ``` 3. 在代码中使用 xUtils3 获取网络图片: ```java ImageView imageView = findViewById(R.id.image_view); String imageUrl = "http://example.com/image.jpg"; x.image().bind(imageView, imageUrl, new Callback.CommonCallback<Drawable>() { @Override public void onSuccess(Drawable result) { // 图片加载成功 } @Override public void onError(Throwable ex, boolean isOnCallback) { // 图片加载失败 } @Override public void onCancelled(CancelledException cex) { // 图片加载被取消 } @Override public void onFinished() { // 图片加载完成 } }); ``` 上面的代码中,我们通过 x.image().bind() 方法来获取网络图片。第一个参数是 ImageView 对象,第二个参数是图片的 URL,第三个参数是一个 Callback,用于监听图片加载的状态。 其中,onSuccess() 方法表示图片加载成功,onError() 方法表示图片加载失败,onCancelled() 方法表示图片加载被取消,onFinished() 方法表示图片加载完成。在实际使用中,我们通常只需要实现 onSuccess() 方法即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值