使用XUtils上传文件(初始版,待整理)

2 篇文章 0 订阅
2 篇文章 0 订阅

import android.os.Environment;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import net.yundongpai.iyd.network.RestApi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * Created by xiaoyee on 9/11/15. In project yundong_android
 */
public class UpDownUtil {


    public static void upload(
//            String filePath, Map<String, String> paramMaps, String url
    ) {

        String uploadUrl = RestApi.getAbsoluteUrl("/yundong/yd_info/addTopicInfo.json");

        File   path    = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        String pathpic = path.getAbsolutePath() + "/Camera/IMG_20140927_113448.jpg";
        String pathVideo = path.getAbsolutePath() + "/Camera/VID_20150911_141955.mp4";
        //sdcard/DCIM/Camera/IMG_20140927_113448.jpg
        RequestParams params = new RequestParams();
        params.addHeader("appversion", "2.2.1(alpha)");
        params.addHeader("build", "26");
        params.addHeader("uid", "10205");
        params.addHeader("User-agent", "Yundong[Android;Model number:Nexus 5;Android version:5.1.1]");

        params.addQueryStringParameter("topic_id", "97");
        params.addQueryStringParameter("type", "2");
        params.addQueryStringParameter("uid", "10205");
        //topic_id 97  type 1

        File filePic = new File(pathpic);
        File fileVideo = new File(pathVideo);
        try {
            InputStream inputStreamPic = new FileInputStream(filePic);
            InputStream inputStreamVideo = new FileInputStream(fileVideo);
//            InputStreamBody body = new InputStreamBody(inputStream, file.length(), file.getName(), "image/jpeg");
//            params.addBodyParameter("file", inputStream, file.length(), file.getName(), "image/jpeg");
            params.addBodyParameter("file_pic", inputStreamPic, filePic.length(), filePic.getName(), "image/jpeg");
            params.addBodyParameter("file_video", inputStreamVideo, fileVideo.length(), fileVideo.getName(), "video/mp4");
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        HttpUtils http = new HttpUtils();
        http.send(
                HttpRequest.HttpMethod.POST,
                uploadUrl,
                params,
                new RequestCallBack<String>() {

                    @Override
                    public void onStart() {
                        LogCus.d("onStart");
                    }

                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                        if (isUploading) {
                            LogCus.d("onLoading  upload: " + current + "/" + total);
//                              testTextView.setText("upload: " + current + "/" + total);
                        } else {
                            LogCus.d("onLoading  reply: " + current + "/" + total);
//                              testTextView.setText("reply: " + current + "/" + total);
                        }
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        LogCus.d("reply: " + responseInfo.result);
//                          testTextView.setText("reply: " + responseInfo.result);
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        LogCus.d("onStart" + error.getExceptionCode() + ":" + msg);
//                          testTextView.setText(error.getExceptionCode() + ":" + msg);
                    }
                }
        );
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值