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

来源:互联网 发布:快手未知网络连接错误 编辑:程序博客网 时间:2024/05/18 02:20
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
原创粉丝点击