android volley ,多文件 表单上传

阅读更多

 

 

实战 QQ demo源码(本例中有该应用)

服务器端下载http://download.csdn.net/download/knight_black_bob/9822551

android eclipse 版http://download.csdn.net/download/knight_black_bob/9822553

android stdio  版本http://download.csdn.net/download/knight_black_bob/9822556

 

 

 

 

android volley ,多文件 表单上传 后台实现  http://knight-black-bob.iteye.com/blog/2311807

 

 

 

 

 

 

public static void createFeed(  FeedEntity feed,  List files){
		final Map params = new HashMap();
		params.put("userFrom", feed.getUserFrom()+"");
		params.put("userNickname", feed.getUserNickname());  
		params.put("userIcon", feed.getUserIcon());      
		params.put("content", feed.getContent());   
		 final Map filemap = new HashMap();
    	 for (File file : files) {
    		 filemap.put("f_file[]", file);
		}
    	String url =  Constants.url_feed_create; 
     	MultipartRequest request = new MultipartRequest(url,   new Listener() {

			@Override
			public void onResponse(String response) { 
				TrendCreateHttpManager.toTrendCreateHttpActionSuccess();
			}
		},new  ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) { 
				TrendCreateHttpManager.toTrendCreateHttpActionError();
			}
		} , "f_file[]", files, params); 
    	 Volley.newRequestQueue(MyApplication.newInstance()).add(request);
	 }

 

 

 

 

volley  MultipartRequest

package com.curiousby.fitnessandappointment.quote.volley;

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: [email protected]
 * @version 创建时间: 2015-08-09 下午4:32
 */
public class MultipartRequest extends Request{

    private MultipartEntity entity = new MultipartEntity();

    private  Response.Listener mListener;

    private List mFileParts;
    private String mFilePartName;
    private Map mParams;

    /**
     * 单个文件+参数 上传
     * @param url
     * @param listener
     * @param errorListener
     * @param filePartName
     * @param file
     * @param params
     */
    public MultipartRequest(String url, Response.Listener listener, Response.ErrorListener errorListener,
                            String filePartName, File file, Map params){
        super(Method.POST, url, errorListener);
        mFileParts = new ArrayList();
        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 listener,Response.ErrorListener errorListener
                            , String filePartName,List files, Map params) {
        super(Method.POST, url, errorListener);
        mFilePartName = filePartName;
        mListener = listener;
        mFileParts = files;
        mParams = params;
        buildMultipartEntity();
    }

    @Override
    protected Response 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 getHeaders() throws AuthFailureError {
        Map headers = super.getHeaders();

        if (headers == null || headers.equals(Collections.emptyMap())) {
            headers = new HashMap();
        }
        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 entry : mParams.entrySet()) {
                    entity.addPart(
                            entry.getKey(),
                            new StringBody(entry.getValue(), Charset
                                    .forName("UTF-8")));
                }
            }
        } catch (UnsupportedEncodingException e) {
            VolleyLog.e("UnsupportedEncodingException");
        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。


android volley ,多文件 表单上传_第1张图片android volley ,多文件 表单上传_第2张图片android volley ,多文件 表单上传_第3张图片
 
 
 谢谢您的赞助,我会做的更好!

 

 

 

 

 

  • android volley ,多文件 表单上传_第4张图片
  • 大小: 1.3 MB
  • 查看图片附件

你可能感兴趣的:(android,volley,多文件,表单,上传)