I have an API that has a payload body of type String. But it has both jsonand image(multi-party system / form) as part of the payload body. Something like that:
json={jsonbody} image=@images/testing.png
This is what I am doing now.
public static String uploadImageQuery(Context context, String urlString, String method,
JSONObject jsonObject, Bitmap largeImageBitmap,
Dialog dialog) throws IOException {
String responseString = null;
HttpURLConnection conn = null;
URL url = new URL(urlString);
conn = (HttpURLConnection) url.openConnection();
Log.d(TAG, "Uploading largeImageBitmap ..");
conn.setConnectTimeout((int) Constants.THREE_MINUTES);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod(method);
conn.setChunkedStreamingMode(16 * 1024);
conn.setRequestProperty("Transfer-Encoding", "chunked");
conn.setRequestProperty("Authorization", "Bearer " + access_token);
conn.connect();
DataOutputStream dataOS = new DataOutputStream(conn.getOutputStream());
dataOS.write(("json=" + jsonObject.toString()).getBytes(Constants.CHARSET_UTF_8));
dataOS.write(("image=").getBytes(Constants.CHARSET_UTF_8));
dataOS.write(buildStartPayload().getBytes(Constants.CHARSET_UTF_8));
dataOS.write(getImageBytes(largeImageBitmap));
dataOS.write(buildEndPayload().getBytes(Constants.CHARSET_UTF_8));
Log.d(TAG, "Posting String data to server : " + dataOS.toString());
dataOS.flush();
dataOS.close();
int responseCode = conn.getResponseCode();
String responseMessage = conn.getResponseMessage();
Log.d(TAG, "Response code for upload image query : " + responseCode + " Message : " + responseMessage);
if (responseCode != 200) {
dialog.cancel();
Log.e(TAG, String.format("Received the response code %d from the URL %s", responseCode, url));
}
InputStream is = conn.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int bytesRead;
while ((bytesRead = is.read(bytes)) != -1) {
baos.write(bytes, 0, bytesRead);
}
byte[] bytesReceived = baos.toByteArray();
baos.close();
is.close();
String response = new String(bytesReceived);
Log.d(TAG, "Response:" + response);
conn.disconnect();
conn = null;
Log.d(TAG, "Cleard the connection handle.");
return responseString;
}
private static String buildStartPayload() {
String contentDisposition = "Content-Disposition: form-data; name=\"testing\"; filename=\"testing.png\"";
String contentType = "Content-Type: image/png";
StringBuilder requestBody = new StringBuilder();
requestBody.append(LINE_START);
requestBody.append(BOUNDARY);
requestBody.append(LINE_END);
requestBody.append(contentDisposition);
requestBody.append(LINE_END);
requestBody.append(contentType);
requestBody.append(LINE_END);
requestBody.append(LINE_END);
return requestBody.toString();
}
private static String buildEndPayload() {
StringBuilder requestBody = new StringBuilder();
requestBody.append(LINE_END + LINE_START + BOUNDARY + LINE_START + LINE_END);
return requestBody.toString();
}
I used HttpURLConnectionto post an image with multiple parts / forms and tried to do this with DataOutputform, but I get a "Bad Request" error. Is there a library that can help? I use the main salvo, but it does not have good image support. I have not tried Retrofit, but I do not want to go there right now. I hope this can be done using HttpURLConnection.