Since API level 22 android deprecate the HTTP Client and Multi part Builder. I want to send some JSON object and images on server in a single request by using MultipartEntityBuilder and HttpURLConnection.
3 Answers
Apache HttpClient 4.3 port for Android was intended to remedy the situation by providing official releases compatible with Google Android.
Given that as of Android API 23 Google's fork of HttpClient has been removed this project has been discontinued.
Those users who want to continue using Apache HttpClient on Android are advised to consider
Apache HttpClient 4.3 port for Android when targeting Android API 22 and older
dependencies {
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
}
Apache HttpClient packages for Android maintained by Marek Sebera when targeting Android API 23 and newer
dependencies {
compile group: 'cz.msebera.android' , name: 'httpclient', version: '4.4.1.1'
}
taken from Apache Official Website : Apache HttpClient for Android
NOTE: You do not have to use useLibrary 'org.apache.http.legacy' statement, which was introduced for projects that didn't migrate from Android provided HttpClient classes. For further explanation.
I have implemented a sample code doing MultipartRequest(File Upload) using Volley.
Comments
I have successfully send the image file and JSON data by using Multipart Entity Builder and HttpOpenUrl connection,by using below code.
String boundary = "*************";
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody fileBody = new FileBody(new File(image));//pass your image path to make a file
builder.addPart("profile_image", fileBody);
builder.addPart("data", new StringBody(jsonObject.toString(), ContentType.TEXT_PLAIN));//pass our jsonObject here
HttpEntity entity = builder.build();
URL url = null;
try {
url = new URL("write your url here");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(10000);
urlConnection.setReadTimeout(10000);
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.addRequestProperty("Content-length", entity.getContentLength() + "");
urlConnection.addRequestProperty(entity.getContentType().getName(), entity.getContentType().getValue());
OutputStream os = urlConnection.getOutputStream();
entity.writeTo(urlConnection.getOutputStream());
os.close();
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String s = "";
StringBuilder stringBuilder = new StringBuilder("");
while ((s = bufferedReader.readLine()) != null) {
stringBuilder.append(s);
}
serverResponseMessage = stringBuilder.toString();
Comments
Together with my comment, because of the deprecation of Apache's librabry from API22, suggest that you use HttpUrlConnection, OkHttp... instead.
You should read more Apache HTTP Client Removal
Relating to OkHttp's sample for multipart requests, please refer to its GitHub documentation here
Hope this helps!