I am developing android application which will upload the image as well as some parameters to the server. From my service side we are using @RequestParam File and @RequestParam getProjectsData as parameters in method in getProjectsData contains all the image details which are stored in the database. Here when i am uploading the image from my android app i am getting an error called HTTP Status 400 - Required request part 'getProjectsData' is not present. Later i had known that i need to send multiple multipart data from my side. Can any one tell me how to post multiple multipart requests one for image upload and another for image properties uploading
This is my android app code
AsyncTask<String, Void, String> uploadImageDetails = new AsyncTask<String, Void, String>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... urls) {
pm_image_details_model pm_img_model = new pm_image_details_model();
pm_img_model.setLatitude(String.valueOf(pm_latitude));
pm_img_model.setLongitude(String.valueOf(pm_longitude));
String result = null;
org.apache.http.entity.mime.MultipartEntity reqEntity = new org.apache.http.entity.mime.MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
// reqEntity.addPart("getProjectsData", new StringBody(""));
bos = new ByteArrayOutputStream();
final Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
ByteArrayBody bab = new ByteArrayBody(data, "test.jpeg");
try {
reqEntity.addPart("file", bab);
reqEntity.addPart("getProjectsData",new StringBody(json));
content = getHttpPutContent(urls[0], reqEntity);
} catch (JSONException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.i("Result from server -->", s);
}
};
HTTP CLIENT
public static String getHttpPutContent(String url, org.apache.http.entity.mime.MultipartEntity multipartEntity) {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost put = new HttpPost(url);
// put.setHeader("Content-Type", "multipart/form-data");
// Log.i(TAG, "Token=> " + token);
Log.i("-->", "Try to open=> " + url);
org.apache.http.entity.mime.MultipartEntity reqEntity = multipartEntity;
put.setEntity(reqEntity);
HttpResponse httpResponse = httpClient.execute(put);
int statusCode = httpResponse.getStatusLine().getStatusCode();
Log.i("-->", "Connection code: " + statusCode);
HttpEntity entity = httpResponse.getEntity();
String serverResponse = EntityUtils.toString(entity);
Log.i("-->", "Server response=> " + serverResponse);
if (!isStatusOk(statusCode))
return null;
return serverResponse;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}