断点续传

package com.zcp.xzsp;

import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

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

 

public class MainActivity extends Activity {

private Button b1;
private String path="101.200.142.201:8080/tqyb/dancer.avi";
private TextView testTextView;

private String h;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.b1);
testTextView= (TextView) findViewById(R.id.tv_info);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File f = Environment.getExternalStorageDirectory();
h=f.getPath();
HttpUtils http = new HttpUtils();
// 如果目标文件存在,接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。
// 如果从请求返回信息中获取到文件名,下载完成后自动重命名。
http.download("http://101.200.142.201:8080/tqyb/dancer.avi",h+"/mj.mp4", true,true,new RequestCallBack<File>(){
@Override
public void onStart() {
testTextView.setText("conn...");
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
testTextView.setText(current + "/" + total);
}
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
testTextView.setText("downloaded:" + responseInfo.result.getPath());
}
@Override
public void onFailure(HttpException error, String msg) {
testTextView.setText(msg);
}
});
}
}
});
}


}

 

//xml布局

 

<RelativeLayout 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"
tools:context=".MainActivity" >

<Button
android:id="@+id/b1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="开始下载"
/>
<TextView
android:id="@+id/tv_info"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="@+id/b1"
android:text="111"
/>
</RelativeLayout>

posted @ 2016-05-03 15:42  夏夜微凉  阅读(103)  评论(0编辑  收藏  举报