android JSON 上传图片和视频到服务器

Cyrusfee 2016-03-16 11:29:22
//上传采集数据到服务器
private void UploadData(final String filename){
new Thread() {
public void run() {
try {
HttpPost httpRequestHttpPost = new HttpPost(HttpUtils.urlpath + "getCollectInfo");
MultipartEntity multipartEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE,
null, Charset.forName(HTTP.UTF_8));
ContentBody contentBody = new FileBody(new File(filename));
multipartEntity.addPart("uploadedfile",contentBody);
multipartEntity.addPart("uid", new StringBody(settings.getInt("user_id", 0)+"",Charset.forName("UTF-8")));
multipartEntity.addPart("node_id", new StringBody(current_node_id,Charset.forName("UTF-8")));
multipartEntity.addPart("info", new StringBody(et_remark.getText().toString(),Charset.forName("UTF-8")));
multipartEntity.addPart("createtime", new StringBody(getTime(),Charset.forName("UTF-8")));
//把要传输的内容放到请求里面
httpRequestHttpPost.setEntity(multipartEntity);
//创建客户端对象
HttpClient httpClient = new DefaultHttpClient();
//执行请求并得到返回结果
HttpResponse httpResponse = httpClient.execute(httpRequestHttpPost);
String ID = EntityUtils.toString(httpResponse.getEntity());
JSONObject json = new JSONObject(ID);
int code = json.getInt("code");
if(code==0){
isupload=true;//上传成功标志
}
System.out.println("uploadpicture========code"+code);
} catch (IOException e) {
System.out.println("IOException========wrong");
e.printStackTrace();
} catch (JSONException e) {
System.out.println("JSONException========wrong");
e.printStackTrace();
}
};
}.start();
}
UploadData("/sdcard/HUA_video/20160316102418.jpg");//图片路径
UploadData("/sdcard/HUA_video/20160316102418.mp4");//视频路径
上面是我上传数据的代码,然后参数是我要上传的路径文件,但是发现图片是可以正常上传的,但是视频出现了System.out.println("IOException========wrong");的情况,麻烦各位帮我看看,小白一个,求大家帮帮忙
...全文
372 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Cyrusfee 2016-03-16
  • 打赏
  • 举报
回复
引用 5 楼 RunningShadow_007 的回复:
[quote=引用 4 楼 w32831973 的回复:] 找到原因了,原来是HttpClient的释放问题,没有马上释放,需要等待超时时间的到来才会自动release掉,所以在上传完之后得加一句HttpResponse.getEntity().consumContent(); 参考自:http://www.mamicode.com/info-detail-889715.html。
遇到问题 看logcat 看什么错误 看什么错误 看什么错误 重要的事情说3遍[/quote] 之前我测得时候发现可以了,现在测又出现那个问题了,log打印出来也是一样的那个
Cyrusfee 2016-03-16
  • 打赏
  • 举报
回复
还是不行啊,原来是可以上传了,但是上传完 过了一会马上又闪退了
RunningShadow_007 2016-03-16
  • 打赏
  • 举报
回复
引用 4 楼 w32831973 的回复:
找到原因了,原来是HttpClient的释放问题,没有马上释放,需要等待超时时间的到来才会自动release掉,所以在上传完之后得加一句HttpResponse.getEntity().consumContent(); 参考自:http://www.mamicode.com/info-detail-889715.html。
遇到问题 看logcat 看什么错误 看什么错误 看什么错误 重要的事情说3遍
Cyrusfee 2016-03-16
  • 打赏
  • 举报
回复
找到原因了,原来是HttpClient的释放问题,没有马上释放,需要等待超时时间的到来才会自动release掉,所以在上传完之后得加一句HttpResponse.getEntity().consumContent(); 参考自:http://www.mamicode.com/info-detail-889715.html。
Cyrusfee 2016-03-16
  • 打赏
  • 举报
回复
引用 2 楼 RunningShadow_007 的回复:
至少要把错误贴出来啊


RunningShadow_007 2016-03-16
  • 打赏
  • 举报
回复
至少要把错误贴出来啊
Cyrusfee 2016-03-16
  • 打赏
  • 举报
回复
顶上去,又没有人帮忙看看啊

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧