Android 使用volley上传图片、多张图片
来源:互联网 发布:php 通过域名获取ip 编辑:程序博客网 时间:2024/06/05 01:53
使用volley请求数据很简便,但遇到上传文件就麻烦那可不好,同时使用多个网络请求类库也是不太建议的。所以这里就给出了一种解决方法,也要借助一个jar包,这里用到的是httpmime(点击下载),主要用到的是MultipartEntity类,可以对请求参数进行封装。
主要是继承volley的Request类,然后通过使用httpmim的MultipartEntity类对文件参数进行封装,这里实现了一个参数名对应一个文件,
一个参数名对应多个文件,如果还要多个参数名对应多个文件可以自己试着实现一下哈
package com.hrtac.testupload;import android.util.Log;import com.android.volley.AuthFailureError;import com.android.volley.NetworkResponse;import com.android.volley.Request;import com.android.volley.Response;import com.android.volley.VolleyLog;import com.android.volley.toolbox.HttpHeaderParser;import org.apache.http.entity.mime.MultipartEntity;import org.apache.http.entity.mime.content.FileBody;import org.apache.http.entity.mime.content.StringBody;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.nio.charset.Charset;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.List;import java.util.Map;/** * 功能: * * @author yanzi E-mail: yanzi1225627@163.com * @version 创建时间: 2015-08-09 下午4:32 */public class MultipartRequest extends Request<String>{ private MultipartEntity entity = new MultipartEntity(); private Response.Listener<String> mListener; private List<File> mFileParts; private String mFilePartName; private Map<String, String> mParams; /** * 单个文件+参数 上传 * @param url * @param listener * @param errorListener * @param filePartName * @param file * @param params */ public MultipartRequest(String url, Response.Listener<String> listener, Response.ErrorListener errorListener, String filePartName, File file, Map<String, String> params){ super(Method.POST, url, errorListener); mFileParts = new ArrayList<File>(); if(file != null && file.exists()){ mFileParts.add(file); }else{ VolleyLog.e("MultipartRequest---file not found"); } mFilePartName = filePartName; mListener = listener; mParams = params; buildMultipartEntity(); } /** * 多个文件+参数上传 * @param url * @param listener * @param errorListener * @param filePartName * @param files * @param params */ public MultipartRequest(String url,Response.Listener<String> listener,Response.ErrorListener errorListener , String filePartName,List<File> files, Map<String, String> params) { super(Method.POST, url, errorListener); mFilePartName = filePartName; mListener = listener; mFileParts = files; mParams = params; buildMultipartEntity(); } @Override protected Response<String> parseNetworkResponse(NetworkResponse response) { String parsed; try { parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); } catch (UnsupportedEncodingException e) { parsed = new String(response.data); } return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response)); } @Override protected void deliverResponse(String response) { mListener.onResponse(response); } @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String, String> headers = super.getHeaders(); if (headers == null || headers.equals(Collections.emptyMap())) { headers = new HashMap<String, String>(); } return headers; } @Override public String getBodyContentType() { return entity.getContentType().getValue(); } @Override public byte[] getBody() throws AuthFailureError { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try{ entity.writeTo(bos); } catch (IOException e) { VolleyLog.e("IOException writing to ByteArrayOutputStream"); } return bos.toByteArray(); } private void buildMultipartEntity() { if (mFileParts != null && mFileParts.size() > 0) { for (File file : mFileParts) { entity.addPart(mFilePartName, new FileBody(file)); } long l = entity.getContentLength(); Log.i("YanZi-volley", mFileParts.size() + "个,长度:" + l); } try { if (mParams != null && mParams.size() > 0) { for (Map.Entry<String, String> entry : mParams.entrySet()) { entity.addPart( entry.getKey(), new StringBody(entry.getValue(), Charset .forName("UTF-8"))); } } } catch (UnsupportedEncodingException e) { VolleyLog.e("UnsupportedEncodingException"); } }}
compile files('libs/volley.jar')compile files('libs/httpcore-4.3.2.jar')compile files('libs/httpclient-4.3.5.jar')compile files('libs/httpmime-4.3.5.jar')
private void doUploadTest(){ mSingleQueue = Volley.newRequestQueue(getApplicationContext()); String path = picPath; Log.e("zb","picPath="+picPath); String url = "http://app.sod90.com/xxx/upload/app_upload"; //换成自己的测试url地址 Map<String, String> params = new HashMap<String, String>(); params.put("id", "19"); params.put("type", "shop"); Log.e("zb","params="+params); File f1 = new File(path); Log.e("zb","f1="+f1.toString());// File f2 = new File(path);// Log.e("zb","f2="+f2.toString()); if(!f1.exists()){ Toast.makeText(getApplicationContext(), "图片不存在,测试无效", Toast.LENGTH_SHORT).show(); return; } List<File> f = new ArrayList<File>(); f.add(f1);// f.add(f2); MultipartRequest request = new MultipartRequest(url, new Response.Listener<String>() { @Override public void onResponse(String response) { Toast.makeText(getApplicationContext(), "uploadSuccess,response = " + response, Toast.LENGTH_SHORT).show(); Log.e("zb", "success,response = " + response); } }, new ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getApplicationContext(), "uploadError,response = " + error.getMessage(), Toast.LENGTH_SHORT).show(); Log.e("zb", "error,response = " + error.getMessage()); } }, "f_file[]", f, params); //注意这个key必须是f_file[],后面的[]不能少 mSingleQueue.add(request); }
通过这个请求就可以上传多文件了,也不做多解释,代码也比较简单。我也对这种方法测试过了,但是由于是和服务器进行打交道,这里确实没有什么可以展示出来,不过我还是喜欢有说服力的描述,所谓有图有真相哈哈,上两个图在做解释
阅读全文
1 0
- Android 使用volley上传图片、多张图片
- 上传一张或多张图片(使用volley框架)
- android 使用OkHttp上传多张图片
- android 使用OkHttp上传多张图片
- android 使用OkHttp上传多张图片
- 使用volley上传图片,一个参数多张图、多张图片多张图,亲测有效哦
- 使用volley上传图片,一个参数多张图、多张图片多张图
- Android 上传多张图片
- Android volley(5)MultipartEntity 封装 volley上传 —— 一个参数多张图、多张图片多张图
- 多张图片上传
- 上传多张图片
- 上传多张图片
- 多张图片上传
- Android使用Retrofit技术仿微信图片上传,可以选择多张图片拍照上传
- Android OkHttp3 上传多张图片
- 使用AFNetworlk上传多张图片
- 使用AFN多张图片上传
- iOS 使用AFNet上传图片(多张)
- SpringBoot集成ActiveMQ
- 深入理解PHP:高级技巧、面向对象与核心技术(原书第3版) -- 设计模式之策略模式
- Python进阶—map函数
- python代码优化
- 分布式和集群的区别
- Android 使用volley上传图片、多张图片
- Unity协程队列,顺序分帧处理
- int、long、long long、unsigned int、_int64的取值范围(与不同位数的编译器有关)
- 利用ajax对页面局部刷新
- 在一个千万级的数据库查寻中,如何提高查询效率?
- 如何让你的浏览器没有广告?
- UE4 Login相关代码
- 【C++ Primer Plus】指针
- bzoj2142 礼物