Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用安卓截取多部分数据_Android_Performance_Android Volley - Fatal编程技术网

Android 如何使用安卓截取多部分数据

Android 如何使用安卓截取多部分数据,android,performance,android-volley,Android,Performance,Android Volley,我正在使用截击调用我的应用程序的web请求。但因为我是第一次截击。我只想知道如何使用多部分通过截取上传图像/视频媒体数据 我在很多网站上搜索了它,最后得到了一些结果 但是,这些方法看起来不好,也不有效。所以,请帮助我,如何上传媒体数据使用截击。或者我不应该使用截击,而应该使用以前的手动方式 无论如何,我们非常感谢所有的想法和答案。谢谢你的帮助 不知道你是否得到了答案,但如果你没有尝试过: import java.io.ByteArrayOutputStream; import java.io.

我正在使用截击调用我的应用程序的web请求。但因为我是第一次截击。我只想知道如何使用多部分通过截取上传图像/视频媒体数据

我在很多网站上搜索了它,最后得到了一些结果

但是,这些方法看起来不好,也不有效。所以,请帮助我,如何上传媒体数据使用截击。或者我不应该使用截击,而应该使用以前的手动方式


无论如何,我们非常感谢所有的想法和答案。谢谢你的帮助

不知道你是否得到了答案,但如果你没有尝试过:

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

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 com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyLog;

public class MultipartRequest extends Request<String> {

// private MultipartEntity entity = new MultipartEntity();

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

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,
        Map<String, String> mStringPart) {
    super(Method.POST, url, 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<String> parseNetworkResponse(NetworkResponse response) {
    return Response.success("Uploaded", getCacheEntry());
}

@Override
protected void deliverResponse(String response) {
    mListener.onResponse(response);
}
   }
import java.io.ByteArrayOutputStream;
导入java.io.File;
导入java.io.IOException;
导入java.util.Map;
导入org.apache.http.HttpEntity;
导入org.apache.http.entity.mime.HttpMultipartMode;
导入org.apache.http.entity.mime.MultipartEntityBuilder;
导入org.apache.http.entity.mime.content.FileBody;
导入com.android.volley.AuthFailureError;
导入com.android.volley.NetworkResponse;
导入com.android.volley.Request;
导入com.android.volley.Response;
导入com.android.volley.VolleyLog;
公共类MultipartRequest扩展请求{
//私有多方实体=新多方实体();
MultipartEntityBuilder实体=MultipartEntityBuilder.create();
HttpEntity HttpEntity;
私有静态最终字符串文件\u PART\u NAME=“FILE”;
私人最终回应。监听器;
私有最终文件mFilePart;
私人最终地图mStringPart;
公共多部分请求(字符串url,Response.ErrorListener ErrorListener,
Response.Listener侦听器,文件,
地图(mStringPart){
super(Method.POST、url、errorListener);
mListener=监听器;
mFilePart=文件;
this.mStringPart=mStringPart;
entity.setMode(HttpMultipartMode.BROWSER_兼容);
buildMultipartEntity();
}
public void addStringBody(字符串参数,字符串值){
mStringPart.put(参数,值);
}
私有void buildMultipartEntity(){
entity.addPart(文件名,新文件体(mFilePart));
对于(Map.Entry:mStringPart.entrySet()){
entity.addTextBody(entry.getKey(),entry.getValue());
}
}
@凌驾
公共字符串getBodyContentType(){
返回httpentity.getContentType().getValue();
}
@凌驾
公共字节[]getBody()抛出AuthFailureError{
ByteArrayOutputStream bos=新建ByteArrayOutputStream();
试一试{
httpentity=entity.build();
httpentity.writeTo(bos);
}捕获(IOE异常){
e(“向ByteArrayOutputStream写入IOException”);
}
返回bos.toByteArray();
}
@凌驾
受保护的响应parseNetworkResponse(NetworkResponse响应){
返回Response.success(“上传”,getCacheEntry());
}
@凌驾
受保护的void deliverResponse(字符串响应){
mListener.onResponse(response);
}
}
为了使其正常工作,您必须使用以下java HttpClient: 在我写这个答案的时候(2014年7月2日),我使用的版本是4.3.2


希望有帮助

您必须使用下面的代码使用多部分截击上传图像。在我的应用程序中,它就像一个符咒

public class MultipartRequest extends Request<String> {

    private MultipartEntity entity = new MultipartEntity();

    private static final String FILE_PART_NAME = "file";
    private static final String STRING_PART_NAME = "text";

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

    public MultipartRequest(String url, Response.ErrorListener errorListener, Response.Listener<String> listener, File file, String stringPart)
    {
        super(Method.POST, url, errorListener);

        mListener = listener;
        mFilePart = file;
        mStringPart = stringPart;
        buildMultipartEntity();
    }

    private void buildMultipartEntity()
    {
        entity.addPart(FILE_PART_NAME, new FileBody(mFilePart));
        try
        {
            entity.addPart(STRING_PART_NAME, new StringBody(mStringPart));
        }
        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<String> parseNetworkResponse(NetworkResponse response)
    {
        return Response.success("Uploaded", getCacheEntry());
    }

    @Override
    protected void deliverResponse(String response)
    {
        mListener.onResponse(response);
    }
}
public类MultipartRequest扩展请求{
私有多方实体=新多方实体();
私有静态最终字符串文件\u PART\u NAME=“FILE”;
私有静态最终字符串字符串\u PART\u NAME=“text”;
私人最终回应。监听器;
私有最终文件mFilePart;
私有最终字符串mStringPart;
公共多部分请求(字符串url、Response.ErrorListener ErrorListener、Response.Listener Listener、文件文件文件、字符串stringPart)
{
super(Method.POST、url、errorListener);
mListener=监听器;
mFilePart=文件;
mStringPart=stringPart;
buildMultipartEntity();
}
私有void buildMultipartEntity()
{
entity.addPart(文件名,新文件体(mFilePart));
尝试
{
entity.addPart(字符串\部件\名称,新的StringBody(mStringPart));
}
捕获(不支持的编码异常e)
{
截击日志(UnsupportedEncodingException);
}
}
@凌驾
公共字符串getBodyContentType()
{
返回实体.getContentType().getValue();
}
@凌驾
公共字节[]getBody()抛出AuthFailureError
{
ByteArrayOutputStream bos=新建ByteArrayOutputStream();
尝试
{
实体书面形式(bos);
}
捕获(IOE异常)
{
e(“向ByteArrayOutputStream写入IOException”);
}
返回bos.toByteArray();
}
@凌驾
受保护的响应parseNetworkResponse(NetworkResponse响应)
{
返回Response.success(“上传”,getCacheEntry());
}
@凌驾
受保护的void deliverResponse(字符串响应)
{
mListener.onResponse(response);
}
}

是的,是的。试试看!工作就像一种魅力,这个答案应该被接受!我的Android项目的添加库有问题。请帮忙。我正在使用eclipse IDE。试着在这里问一个新问题,描述一下你的问题,你尝试了什么。@Maoheeng你应该看看开始迁移到Android Studio。随进度上传