0

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.

2
  • 2
    MultipartEntityBuilder is also deprecated in API23 :) Commented Oct 9, 2015 at 15:58
  • Than what should be used ? @BNK Commented Oct 9, 2015 at 16:03

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.