问题描述
我想从我的android应用程序上传文件(图片,文档等)到我的服务器上。我在我的应用程序中使用Volley库进行网络调用。我使用下面的代码来上传文件,但是它没有执行任何操作(最后显示排气超时错误)
public MultipartRequest类扩展了Request< String> {
private MultipartEntity entity = new MultipartEntity();
private static final String FILE_PART_NAME =file;
private final Response.Listener< String> mListener;
private final File mFilePart;
private String mStringPart,accessToken;
public MultipartRequest(String url,Response.ErrorListener errorListener,Response.Listener< String> listener,File file,String accessToken)
{
super(Method.POST,url, errorListener);
mListener = listener;
mFilePart = file;
this.accessToken = accessToken;
buildMultipartEntity();
private void buildMultipartEntity()
{
entity.addPart(FILE_PART_NAME,new FileBody(mFilePart));
尝试
{
entity.addPart(Content-Disposition,new StringBody(form-data));
entity.addPart(dir_path,new StringBody(IzEzOjE3));
catch(UnsupportedEncodingException e)
{
VolleyLog.e(UnsupportedEncodingException);
}
}
@Override
public Map< String,String> getHeaders()抛出AuthFailureError {
Map< String,String> headers = super.getHeaders();
if(headers == null
|| headers.equals(Collections.emptyMap())){
headers = new HashMap< String,String>();
headers.put(Content-Type,multipart / form-data);
headers.put(_ pkta,accessToken);
返回头文件;
@Override
public String getBodyContentType()
{
return entity.getContentType()。getValue();
$ b @Override
public byte [] getBody()throws AuthFailureError
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
尝试
{
entity.writeTo(bos);
}
catch(IOException e)
{
VolleyLog.e(IOException写入ByteArrayOutputStream);
}
return bos.toByteArray();
}
@Override
受保护的响应< String> parseNetworkResponse(NetworkResponse response)
{
return Response.success(Uploaded,getCacheEntry());
$ b @Override
protected void deliverResponse(String response)
{
mListener.onResponse(response);
$ / code $ / pre
请帮助我如何在android中实现文件上传(或者通过抽签或者任何其他方式)
解决方案试试
<$ p $ ($)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $。
entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
file = new File(filePath);
mListener = listener;
buildMultipartEntity();
$ b
在我的情况下,我没有重写getHeaders,只是传递了其他值使用addPart,例如:
$ b $ pre $ entity.addPart(file,new FileBody(file,image / jpeg)); //对于图像
entity.addPart(id,new StringBody(userid));
希望这有助于您。
I want to upload files(images,documents etc) from my android application to my server.I'm using Volley library for network calls in my application.
I'm using the following code to upload files but it is not performing any operation.(showing volley timeout error finally)
public class MultipartRequest extends Request<String> {
private MultipartEntity entity = new MultipartEntity();
private static final String FILE_PART_NAME = "file";
private final Response.Listener<String> mListener;
private final File mFilePart;
private String mStringPart,accessToken;
public MultipartRequest(String url, Response.ErrorListener errorListener, Response.Listener<String> listener, File file,String accessToken)
{
super(Method.POST, url, errorListener);
mListener = listener;
mFilePart = file;
this.accessToken = accessToken;
buildMultipartEntity();
}
private void buildMultipartEntity()
{
entity.addPart(FILE_PART_NAME, new FileBody(mFilePart));
try
{
entity.addPart("Content-Disposition", new StringBody("form-data"));
entity.addPart("dir_path", new StringBody("IzEzOjE3"));
}
catch (UnsupportedEncodingException e)
{
VolleyLog.e("UnsupportedEncodingException");
}
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = super.getHeaders();
if (headers == null
|| headers.equals(Collections.emptyMap())) {
headers = new HashMap<String, String>();
}
headers.put("Content-Type", "multipart/form-data");
headers.put("_pkta",accessToken);
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();
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response)
{
return Response.success("Uploaded", getCacheEntry());
}
@Override
protected void deliverResponse(String response)
{
mListener.onResponse(response);
}
}
please help me on how to implement file upload in android(either by volley or any other)
解决方案 Try
public MultiPartRequest(String url, String filePath, Response.Listener<String> listener, Response.ErrorListener errorListener)
{
super(Method.POST, url, errorListener);
entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
file = new File(filePath);
mListener = listener;
buildMultipartEntity();
}
also, in my case I didn't override getHeaders, just passed other values with addPart, e.g:
entity.addPart("file", new FileBody(file, "image/jpeg")); // for image
entity.addPart("id", new StringBody(userid));
hope this helps.
这篇关于如何在Android中使用Volley执行文件上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!