gpt4 book ai didi

android - 在android中使用Volley Library上传多张图片

转载 作者:太空狗 更新时间:2023-10-29 14:16:30 25 4
gpt4 key购买 nike

我尝试在单个 web 服务 中使用 volley library 上传多张图片。但是,只有最后一张图片正在上传。那些以前的图像正在被 null 替换。我想知道 volley 库是否可行,如果不行,请向我推荐其他库。我是 android 的新手,我只知道 volley 上传图片。

//JSON请求

public MySampleImageUpload() {
JSONRequestResponse mResponse = new JSONRequestResponse(mContext);

Bundle parms = new Bundle();
parms.putString("key_meail", "rojesh@demo.com");
parms.setFile("key_url", image_path);

mResponse.getResponse("sample_upload_data_url", REQUEST_CODE, this,
parms);

}

//SetFile & getResponse代码中

package com.fartogram.utils;

import java.io.File;

import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.examples.toolbox.MultipartRequest;
import com.android.volley.examples.toolbox.MyVolley;
import com.android.volley.toolbox.JsonObjectRequest;

public class JSONRequestResponse {

public JSONRequestResponse(Context cntx) {
mContext = cntx;
}

private final Context mContext;
private int reqCode;
private IParseListener listner;

private boolean isFile = false;
private String file_path = "", key = "";

public void getResponse(String url, final int requestCode,
IParseListener mParseListener) {
getResponse(url, requestCode, mParseListener, null);
}

public void getResponse(String url, final int requestCode,
IParseListener mParseListener, Bundle params) {
this.listner = mParseListener;
this.reqCode = requestCode;

Response.Listener<JSONObject> sListener = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (listner != null) {
listner.SuccessResponse(response, reqCode);
}
}
};

Response.ErrorListener eListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (listner != null) {
listner.ErrorResponse(error, reqCode);
}
}
};

if (!isFile) {
JsonObjectRequest jsObjRequest = new JsonObjectRequest(
Request.Method.GET, url, null, sListener,

eListener);
MyVolley.getRequestQueue().add(jsObjRequest);
} else {
if (file_path != null) {
File mFile = new File(file_path);
MultipartRequest multipartRequest =
new MultipartRequest(url,eListener, sListener, key, mFile, params);
MyVolley.getRequestQueue().add(multipartRequest);
}
}
}

public boolean isFile() {
return isFile;
}


public void setFile(String param, String path) {
if (path != null && param != null) {
key = param;
file_path = path;
this.isFile = true;
}
}

}

最佳答案

public class CustomMultiRequest extends Request<JSONObject> {

private MultipartEntity entity = new MultipartEntity();

private static final String FILE_PART_NAME = "postfieldname[]";

private final Response.Listener<JSONObject> mListener;
private final ArrayList<File> mFilePart;
private final ArrayList<PostEntityModel> mStringPart;

public CustomMultiRequest(String url, Response.ErrorListener errorListener, Response.Listener<JSONObject> listener,ArrayList<File> files, ArrayList<PostEntityModel> stringPart)
{
super(Method.POST, url, errorListener);
mListener = listener;
mFilePart = files;
mStringPart = stringPart;
buildMultipartEntity();
}

private void buildMultipartEntity()
{
for (File file : mFilePart){
entity.addPart(FILE_PART_NAME, new FileBody(file));
}
try
{
for(PostEntityModel postEntityModel : mStringPart){
entity.addPart(postEntityModel.getName(), new StringBody(postEntityModel.getValue()));
}
}
catch (UnsupportedEncodingException e)
{
VolleyLog.e("UnsupportedEncodingException");
}
}

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

@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
protected void deliverResponse(JSONObject response)
{
mListener.onResponse(response);
}
}

如果您使用 Gradle,请将此添加到您的 build.gradle 文件中:

 compile('org.apache.httpcomponents:httpmime:4.3.6') {
exclude module: 'httpclient'
}
compile 'org.apache.httpcomponents:httpclient-android:4.3.5'

关于android - 在android中使用Volley Library上传多张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21448003/

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