2013-08-17 32 views
29

Tôi đang sử dụng Volley để gọi yêu cầu web cho ứng dụng của tôi. Nhưng khi tôi là Volley lần đầu tiên. Tôi chỉ muốn biết rằng làm thế nào để tải lên hình ảnh/phương tiện truyền thông dữ liệu thông qua bóng chuyền bằng cách sử dụng multipart.Cách đa dữ liệu bằng Android Volley

Tôi đã tìm kiếm cho nó nhiều trang web, tôi có một số kết quả trên

How to send a “multipart/form-data” POST in Android with Volley

Nhưng, những phương pháp does'nt nhìn tốt hoặc efficients. Vì vậy, hãy giúp tôi rằng làm thế nào để tải lên dữ liệu phương tiện truyền thông bằng cách sử dụng bóng chuyền. Hoặc tôi không nên sử dụng Volley và phải đi theo hướng dẫn trước đó

Dù sao, tất cả các suy nghĩ và câu trả lời đều được đánh giá cao. Cảm ơn sự giúp đỡ của bạn.

+0

Tải lên với tiến trình http://stackoverflow.com/a/28144660/185022 –

Trả lời

28

Không biết nếu bạn đã có một câu trả lời, nhưng nếu bạn đã không cố gắng này:

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

Để làm cho nó hoạt bạn phải sử dụng java này httpclients: http://hc.apache.org/downloads.cgi Bởi thời gian Tôi đang viết câu trả lời này (02/07/14) phiên bản tôi đang sử dụng là 4.3.2.

Hy vọng điều đó sẽ hữu ích!

+0

thực hiện công việc này ??? –

+0

Vâng, đúng vậy. Hãy thử một lần ! – Leonardo

+0

Hoạt động như một sự quyến rũ, câu trả lời này phải được chấp nhận! –

13

Bạn phải sử dụng mã bên dưới để tải lên hình ảnh bằng cách sử dụng multipart với volley. Nó hoạt động như một nét duyên dáng trong ứng dụng của tôi.

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