使用XUtils上传文件(初始版,待整理)
2015-09-11 19:03
302 查看
[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); } } ); } }
相关文章推荐
- VC多线程编程
- NullReferenceException: Object reference not set to an instance of an object
- 减少mssql2008数据库的ldf文件
- 关于Scanner类写在循环外面抛出异常后会无限循环的问题
- 用mocha写分页的测试用例
- iOS自定义NavigationBar
- 通过命令编译的项目 导入 到eclipse
- 如何获取短信验证码
- BitmapShader初步认识
- 处理样本分布不平衡,偏斜比较厉害的方法总结
- JAVA- 学习日记-命令行运行java程序
- 详解C++编程中用数组名作函数参数的方法
- 使用Keychain存储用户敏感信息
- KL46 custom board SWD reset is never asserted - SWS Waveform
- Centos7 安装kilo-5 controller(nova)
- 无聊
- linux下tty,控制台,虚拟终端,串口,console(控制台终端)详解http://blog.csdn.net/liaoxinmeng/article/details/5004743
- iOS开发-------简单通讯录(UITableView和CoreData的应用)
- HTTP协议详解
- win2d绘制图片