gpt4 book ai didi

java - 如何使用 Volley 在 Android 中发送多部分请求

转载 作者:行者123 更新时间:2023-11-30 01:54:20 24 4
gpt4 key购买 nike

我有一些代码可以使用 Android Volley 发送多部分请求。但是当我尝试运行代码时,显示错误“BasicNetwork.performRequest:意外响应代码 401”。

希望,任何人都可以帮助我。谢谢。

这是我的多方请求。

public class MultipartRequest extends Request<String> {
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
HttpEntity httpentity;
private String FILE_PART_NAME = "upload";

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

public MultipartRequest(String url, Response.ErrorListener errorListener,
Response.Listener<String> listener, File file,
long length, Map<String, String> mStringPart, HashMap<String, String> headers, HashMap<String, String> params, Object o) {
super(Method.POST, url, errorListener);

this.mListener = listener;
this.mFilePart = file;
this.mStringPart = mStringPart;

entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
try {
entity.setCharset(CharsetUtils.get("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
buildMultipartEntity();
httpentity = entity.build();
}

private void buildMultipartEntity() {
entity.addPart(FILE_PART_NAME, new FileBody(mFilePart, ContentType.create("image/jpeg"), mFilePart.getName()));
if (mStringPart != null) {
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.writeTo(bos);
}
catch (IOException e)
{
VolleyLog.e("IOException writing to ByteArrayOutputStream");
}
return bos.toByteArray();
}

@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {

try {
System.out.println("Network Response "+ new String(response.data, "UTF-8"));
return Response.success(new String(response.data, "UTF-8"),
getCacheEntry());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return Response.success(new String(response.data), getCacheEntry());
}
}

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

我这样调用多部分请求。

 private void upload(){
HashMap<String, String> headers = new HashMap<String, String>();
String credentials = AppConfig.USER_API+":"+AppConfig.PASSWORD_API;
String auth = "Basic "
+ Base64.encodeToString(credentials.getBytes(),
Base64.NO_WRAP);
headers.put("Authorization", auth);

File sourceFile = new File(filePath);

HashMap<String, String> params = new HashMap<String, String>();
params.put("desc", "OK");

MultipartRequest multipartRequest = new MultipartRequest(AppConfig.URL_REPORT,

new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Failed : " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
}

},
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(),
"Success", Toast.LENGTH_LONG)
.show();
}

}, sourceFile, sourceFile.length(), null, headers, params, null);
VolleyController.getInstance().addToRequestQueue(multipartRequest);
}

最佳答案

如评论所述,我找不到您在哪里处理 MultipartRequest 类中的 header

所以像这样更新你的类:

public class MultipartRequest extends Request<String>{

...
private final Map<String, String> mHeaders;
...

public MultipartRequest(..., Map<String, String> headers, ...) {
super(...);
...
this.mHeaders = headers;
...
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return (mHeaders != null) ? mHeaders : super.getHeaders();
}

...
}

关于java - 如何使用 Volley 在 Android 中发送多部分请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32513457/

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