使用volley上传视频

时间:2015-11-11 06:56:42

标签: java php android upload android-volley

在我的应用中,用户应该能够将他们的视频从手机上传到远程服务器。我不想使用像this这样的方法,我必须处理所有事情以及许多其他缺点。是否可以使用截击这个目标?如果是,请解释如何或提及教程。

1 个答案:

答案 0 :(得分:1)

  

您可以使用以下代码段来处理任何多部分请求   截击。

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.HttpHeaderParser;

import org.apache.http.HttpEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

public class TestMultiPartRequestextends Request<JSONObject> {

// private MultipartEntity entity = new MultipartEntity();

    MultipartEntityBuilder entity = MultipartEntityBuilder.create();
    HttpEntity httpentity;
    private static final String FILE_PART_NAME = "import_file";

    private final Response.Listener<JSONObject> mListener;
    private final Response.ErrorListener mErrorListener;
    private final File mFilePart;
    private final Map<String, String> mStringPart;

    public TestMultiPartRequest(String url, Response.ErrorListener errorListener,
                                 Response.Listener<JSONObject> listener, File file,
                                 Map<String, String> mStringPart) {
        super(Method.POST, url, errorListener);
        this.mErrorListener = errorListener;
        mListener = listener;
        mFilePart = file;
        this.mStringPart = mStringPart;
        entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        buildMultipartEntity();
    }

    public void addStringBody(String param, String value) {
        mStringPart.put(param, value);
    }

    private void buildMultipartEntity() {
        entity.addPart(FILE_PART_NAME, new FileBody(mFilePart));
        for (Map.Entry<String, String> entry : mStringPart.entrySet()) {
            entity.addTextBody(entry.getKey(), entry.getValue());
        }
    }

    @Override
    public String getBodyContentType() {
        return httpentity.getContentType().getValue();
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            httpentity = entity.build();
            httpentity.writeTo(bos);
        } catch (IOException e) {
            VolleyLog.e("IOException writing to ByteArrayOutputStream");
        }
        return bos.toByteArray();
    }

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }

    @Override
    public void deliverError(VolleyError error) {
        mErrorListener.onErrorResponse(error);
    }

    @Override
    protected void deliverResponse(JSONObject response) {
        mListener.onResponse(response);
    }
}
  

用法

TestMultiPartRequest multiPartRequest = new TestMultiPartRequest (URL, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
               //Error response
            }
        }, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
               //Response
            }
        }, new File(fPath), params);

        multiPartRequest.setRetryPolicy(new DefaultRetryPolicy(5 * 1000, 0,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        TestVolleyHelper.getInstance(getApplicationContext()).addToRequestQueue(multiPartRequest);
  

TestVolleyHelper

import android.content.Context;
import android.text.TextUtils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;


public class TestVolleyHelper {

    private static final String TAG = TestVolleyHelper.class
            .getSimpleName();

    private RequestQueue mRequestQueue;
    private static TestVolleyHelper mInstance;

    public TestVolleyHelper(Context context) {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(context);
        }
    }

    public static synchronized TestVolleyHelper getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new TestVolleyHelper(context);
        }
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        // set the default tag if tag is empty
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }
}

看看this是一个不使用HttpEntity的示例。

并且没有使用HttpEntity。它已在Android-M中完全删除,因为它最近已被弃用。