android客户端多文件上传(最简单和强悍版本),一看就懂
2016-03-15 09:44
633 查看
一、本文本主要解决客户端中一次性上传多个文件的代码整理
1、直接上代码
private void fileuploadMore1(ArrayList<String> imgName, String uid,
String title, String context) throws URISyntaxException,
UnsupportedEncodingException {
// StringEntity reqEntity = new StringEntity(context, HTTP.UTF_8);
String actionUrl = HttpUrl.actionUrl + "?UID=" + uid + "&SUBJECT="
+ URLEncoder.encode(title, "UTF-8") + "&CONTENT="
+ URLEncoder.encode(context, "UTF-8");
// actionUrl= URLEncoder.encode(actionUrl, "UTF-8");
URI url = new URI(actionUrl);
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
MultipartEntity multiEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE, null, null);
for (String file : imgName) {
FileBody body = new FileBody(new File(file));// (FileUrl +file));
multiEntity.addPart(file, body);
}
post.setEntity(multiEntity);
try {
System.out.println("data-0");
HttpResponse response = client.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("data-1");
String result = EntityUtils.toString(response.getEntity(), "utf-8");
JSONObject jsObject = null;
try {
jsObject = new JSONObject(result);
} catch (JSONException e1) {
e1.printStackTrace();
}
if (statusCode == 200) {
System.out.println("data-1" + "success");
handler.sendEmptyMessage(0x01);
} else {
System.out.println("data-1" + "fail else");
handler.sendEmptyMessage(0x02);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
System.out.println("data-1" + "fail e1");
handler.sendEmptyMessage(0x02);
} catch (IOException e) {
e.printStackTrace();
System.out.println("data-1" + "fail e2");
handler.sendEmptyMessage(0x02);
}
}
说明 actionUrl 写你自己服务器端支持图片多文件上传接口,这个需要服务器端一起配合,我这只提供客户端代码
2 需要增加架包httpmime 架包。
3 使用非常简单,而且上传功能实现非常好
1、直接上代码
private void fileuploadMore1(ArrayList<String> imgName, String uid,
String title, String context) throws URISyntaxException,
UnsupportedEncodingException {
// StringEntity reqEntity = new StringEntity(context, HTTP.UTF_8);
String actionUrl = HttpUrl.actionUrl + "?UID=" + uid + "&SUBJECT="
+ URLEncoder.encode(title, "UTF-8") + "&CONTENT="
+ URLEncoder.encode(context, "UTF-8");
// actionUrl= URLEncoder.encode(actionUrl, "UTF-8");
URI url = new URI(actionUrl);
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
MultipartEntity multiEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE, null, null);
for (String file : imgName) {
FileBody body = new FileBody(new File(file));// (FileUrl +file));
multiEntity.addPart(file, body);
}
post.setEntity(multiEntity);
try {
System.out.println("data-0");
HttpResponse response = client.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("data-1");
String result = EntityUtils.toString(response.getEntity(), "utf-8");
JSONObject jsObject = null;
try {
jsObject = new JSONObject(result);
} catch (JSONException e1) {
e1.printStackTrace();
}
if (statusCode == 200) {
System.out.println("data-1" + "success");
handler.sendEmptyMessage(0x01);
} else {
System.out.println("data-1" + "fail else");
handler.sendEmptyMessage(0x02);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
System.out.println("data-1" + "fail e1");
handler.sendEmptyMessage(0x02);
} catch (IOException e) {
e.printStackTrace();
System.out.println("data-1" + "fail e2");
handler.sendEmptyMessage(0x02);
}
}
说明 actionUrl 写你自己服务器端支持图片多文件上传接口,这个需要服务器端一起配合,我这只提供客户端代码
2 需要增加架包httpmime 架包。
3 使用非常简单,而且上传功能实现非常好
相关文章推荐
- Android 如何在service和BroadCastReceiver中 启动activity和alertdialog?
- Android 判断应用是否在运行以及判断处于顶层的activity
- android 禁止通知栏下拉
- 2016年3月15日Android实习日记
- Ecplise 更新代码出错:Description Resource Path Location Type The container 'Android Dependencies'
- 【Gallery3D】播放视频时上下文菜单的设置
- Android - NetworkInfo对象的isConnected()与isAvailable()
- Android 通知栏Notification
- Android开发之动画效果浅析(一)
- Android动画效果——X、Y轴抖动
- android如何让控件摆放在屏幕底部
- Android将Log写入文件
- Android——配置环境变量
- Android N预览版 权限上的一些改变
- AndroidAlbum 跳转到相册或者调用系统拍照的功能
- Android中自定义控件之液位指示器
- Android性能优化典范(四) google 推出 mark
- Android——寄存器和存储器的区别
- Android安装NDK运行环境无需cygwin
- Android View总结