使用XUtils上传文件(初始版,待整理)
2015-09-11 19:12
295 查看
[code] 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); } } ); } }
相关文章推荐
- 使用Mob短信sdk遇到的问题,解决
- 做人大忌
- 沙盒 与数据本地化
- 分享一百多套开发视频教程的下载地址
- a标签禁用onclick事件,60秒后生效
- iOS动态计算UIScrollView的contentSize示例(页面由Label组成)
- html标签页图标
- AD 升级
- 维基百科简体中文语料的获取
- CentOS 6.5 配置静态IP
- java 斗地主的写法
- 微信验证码
- 最新新闻:电商黑色产业链 贩卖手机验证码刷销量
- 最新新闻:电商黑色产业链 贩卖手机验证码刷销量
- iOS登陆各种验证(持续更新)
- 深入浅出CChart 每日一课——快乐高四第十六课 老马识途,ChartCtrl控件在WTL中的使用
- UIActivityIndicatorView的使用
- mysql共享表空间和独立表空间
- iOS每日一记之————AlertView的BUG解决 限制textField的字数长度 ScrollerView的上划键盘下去
- 聚合数据Android SDK 短信验证演示示例