afinal上传下载

项目中要用到上传下载,不想自己写了,就用了afinal框架,下边是上传下载的代码,高手绕道



package com.example.testfile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import net.tsz.afinal.http.HttpHandler;

import org.json.JSONException;
import org.json.JSONObject;


import android.net.Uri;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
TextView t;
Button b;
Button b3;
TextView t1;
Button b1;
EditText e;

HttpHandler handler ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t=(TextView)findViewById(R.id.textView1);
b=(Button)findViewById(R.id.button1);
t1=(TextView)findViewById(R.id.textView2);
b1=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
e=(EditText)findViewById(R.id.editText1);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
t.setText("");
showChooser();

}
});
b3.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
 handler.stop();

}
});
b1.setOnClickListener(new OnClickListener() {

@SuppressLint("SdCardPath")
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void onClick(View v) {
   FinalHttp fh = new FinalHttp();  
    //调用download方法开始下载

handler = fh.download("http://my-apps.bj.bcebos.com/test/F180K_20C%E5%9F%BA%E5%B8%A6.rar?responseContentDisposition=attachment&authorization=bce-auth-v1%2Fb2e826e8a0c24a09a0381d75527d06c6%2F2015-07-31T01%3A48%3A36Z%2F30000%2Fhost%2F74a74018abdc0d97747ef3c5043aca5ac611c117bd34937ff92935cb1186d364", //这里是下载的路径
    "/mnt/sdcard/testapk.rar",//true:断点续传 false:不断点续传(全新下载)
    true, //这是保存到本地的路径
    new AjaxCallBack() {  
                @Override  
                public void onLoading(long count, long current) {  
                     t1.setText("下载进度:"+(int)((double)current/(double)count*100)+"%");  
                }  

              

@SuppressWarnings("unused")
public void onSuccess(File f) {  
                 System.out.println(isProgress()+"");
                    t1.setText(f==null?"null":f.getAbsoluteFile().toString());  
                }  

            });  
 
   //调用stop()方法停止下载
 // 

}
});
}

//显示文件选择
private void showChooser() {

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent, "选择文件"),
2);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "请安装文件管理器", Toast.LENGTH_SHORT)
.show();
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, final Intent data) {

System.out.println("请选择小于1文件");

if (data != null) {
Uri uri = data.getData();
try {

String path = FileUtils.getPath(this, uri);
File tempfile=new File(path);
if(tempfile.length()>9480000){
System.out.println("请选择小于10M的文件");
return;
}

AjaxParams params = new AjaxParams();
//  params.put("username", "michael yang");
//  params.put("password", "123456");
//  params.put("email", "test@tsz.net");
  params.put("profile_picture", tempfile); // 上传文件
 // params.put("profile_picture2", inputStream); // 上传数据流
 // params.put("profile_picture3", new ByteArrayInputStream(10)); // 提交字节流


  FinalHttp fh = new FinalHttp();
  fh.configTimeout(1000*60*10);
 
  fh.post("这里是上传地址", 
  params,
  new AjaxCallBack<Object>(){
        @Override
        public void onLoading(long count, long current) {
                t.setText((int)((double)current/(double)count*100)+"%");
                System.out.println((int)((double)current/(double)count*100)+"%");
        }

      

@Override
public void onSuccess(Object s) {
// TODO Auto-generated method stub
          t.setText(s==null?"null":s.toString());
}


  });


} catch (Exception e) {
}


super.onActivityResult(requestCode, resultCode, data);
}}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值