gpt4 book ai didi

java - 使用volley上传视频

转载 作者:行者123 更新时间:2023-12-01 10:55:09 25 4
gpt4 key购买 nike

在我的应用程序中,用户应该能够将视频从手机上传到远程服务器。我不想使用像 this 这样的方法在那里我必须照顾一切以及许多其他缺点。是否可以使用 Volley 来达到此目的?如果是,请解释如何操作或提及教程。

最佳答案

You can use the following snippet for any multipart request using volley.

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);
}
}

Usage

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 中被完全删除,因为它最近已被弃用。

关于java - 使用volley上传视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33645620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com