के माध्यम से सीमा के साथ मल्टीपार्ट फ़ाइल भेजने में समस्या मेरे पास मानक अपाचे कक्षाओं का उपयोग कर एक ग्राहक HTTP कॉल काम कर रहा है लेकिन मैं इसे संभालने के लिए कस्टम वॉली क्लास बनाने की कोशिश कर रहा हूं। यहाँ मानक कॉल के लिए कोड है:वॉली
HttpURLConnection conn = (HttpURLConnection) new URL(strUrl).openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(30000);
conn.setUseCaches(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Token " + m_apiKey);
conn.setRequestProperty("Accept", "text/plain , application/json");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + strBoundary);
conn.connect();
// **** Start content wrapper:
DataOutputStream request = new DataOutputStream(conn.getOutputStream());
request.writeBytes("\r\n--" + strBoundary + "\r\n");
request.writeBytes("Content-Disposition: form-data; name=\"" + attachmentName + "\";filename=\"" + imageFileName + "\"" + "\r\n");
request.writeBytes("Content-Type: image/jpeg " + "\r\n");
request.writeBytes("\r\n");
request.write(baos.toByteArray());
// **** End content wrapper:
request.writeBytes("\r\n--"+ strBoundary + "--\r\n");
// Flush output buffer:
request.flush();request.close();
कैसे आराम करने के लिए सुनिश्चित नहीं हैं, लेकिन यह है कि मैं क्या वॉली कि काम नहीं कर रहा के लिए है। मैंने पहले मल्टीपार्ट किया है लेकिन सीमा के साथ नहीं और इस अजीब प्रारूप में।
सार्वजनिक वर्ग ImageMultiRequest StringRequest { अंतिम स्ट्रिंग BOUNDARY = "कुछ" बढ़ाता है; अंतिम स्ट्रिंग crlf = "\ r \ n"; अंतिम स्ट्रिंग दो हाइफेंस = "-";
private final MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
private Response.Listener<String> mListener = null;
private Response.ErrorListener mEListener;
//
private final File mFilePart;
private Map<String, String> parameters;
private Map<String, String> header;
MultipartEntity entity = new MultipartEntity();
protected String apiKey;
ByteArrayOutputStream bos;
public ImageMultiRequest(String apiKey, String url, Listener<String> rListener, ErrorListener eListener, File file) {
super(Method.POST, url, rListener, eListener);
setShouldCache(false);
this.apiKey = apiKey;
this.bos = bos;
mListener = rListener;
mEListener = eListener;
mFilePart = file;
entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entityBuilder.setBoundary(BOUNDARY);
buildMultipartEntity();
}
@Override
public String getBodyContentType() {
return "multipart/form-data; boundary=" + BOUNDARY + "; charset=utf-8";
}
/**
* Overrides the base class to add the Accept: application/json header
*/
@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;boundary=" + BOUNDARY+ "; charset=utf-8");
headers.put("Connection", "Keep-Alive");
headers.put("Accept", "text/plain , application/json");
headers.put("Authorization", "Token " + apiKey);
return headers;
}
@Override
public byte[] getBody() throws AuthFailureError {
buildMultipartEntity();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
entityBuilder.build().writeTo(bos);
} catch (IOException e) {
VolleyLog.e("IOException writing to ByteArrayOutputStream");
}
return bos.toByteArray();
}
private void buildMultipartEntity() {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
entityBuilder.addPart("Content-Type: image/jpeg " + crlf + crlf, new ByteArrayBody(bos.toByteArray(), "car"));
}
}
=========================================== ======
समाधान नीचे
यहाँ जवाब के आधार पर मैं क्या आया कि के साथ इस के लिए काम किया है और संभवतः बहुखण्डीय फ़ाइल के साथ अन्य समस्याओं सामग्री शरीर
package com.cars.android.common.volley;
import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.StringRequest;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class ImageMultiRequest extends StringRequest {
final String BOUNDARY = "myboundary";
private final MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
HttpEntity entity = new MultipartEntity();
private Response.Listener<String> mListener = null;
private Response.ErrorListener mEListener;
//
private final File mFilePart;
protected String apiKey;
public ImageMultiRequest(String apiKey, String url, Listener<String> rListener, ErrorListener eListener, File file) {
super(Method.POST, url, rListener, eListener);
setShouldCache(false);
this.apiKey = apiKey;
mListener = rListener;
mEListener = eListener;
mFilePart = file;
entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entityBuilder.setBoundary(BOUNDARY);
ContentType contentType = ContentType.create("image/png");
entityBuilder.addBinaryBody("file", file, contentType, "car");
entity = entityBuilder.build();
}
@Override
public String getBodyContentType() {
return entity.getContentType().getValue();
}
/**
* Overrides the base class to add the Accept: application/json header
*/
@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;boundary=" + BOUNDARY+ "; charset=utf-8");
headers.put("Connection", "Keep-Alive");
headers.put("Accept", "text/plain , application/json");
headers.put("Authorization", "Token " + apiKey);
return headers;
}
@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();
}
}
हे समाधान के लिए धन्यवाद, मुझे एक टन बचाया, आपके सम्मान में चीयर्स! (एक ग्लास उठा रहा है) – JPM