2

I am converting my project in Studio from eclipse.I am sending image in multi part to sever using httpmime 4.5.1 jar file. This is my code.it gives me NoClassDefFoundError: org.apache.http.Consts error.

Please help me.

This is my code.

new AsyncTask<String, Integer, Void>() {



                @Override
                protected void onPreExecute() {

                   // setting progress bar to zero
                    progressBar.setProgress(0);

                    super.onPreExecute();

                }

                @Override
                protected void onProgressUpdate(Integer... progress) {
                    // Making progress bar visible

                    // updating progress bar value
                    progressBar.setProgress(progress[0]);

                    txtPercentage.setText(progress[0] + "%");

                }

                @SuppressWarnings("deprecation")
                @Override
                protected Void doInBackground(String... params) {



                    for (i = 0; i < allPath.length; i++) {



                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                txtCount.setText((i + 1) + "/"
                                        + allPath.length);

                            }
                        });

                        allPath[i] = selected.get(i).sdcardPath;





                        String responseString = null;

                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost(
                                Config.FILE_UPLOAD_URL);

                        try {

                            AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
                                    new ProgressListener() {

                                        @Override
                                        public void transferred(long num) {
                                            publishProgress((int) ((num / (float) totalSize) * 100));
                                        }
                                    });

                            File sourceFile = new File(
                                    compressImage(allPath[i]));

                            // Adding file data to http body
                            entity.addPart("tag", new StringBody(
                                    "productAdd"));

                            // Adding file data to http body
                            entity.addPart("image",
                                    new FileBody(sourceFile));

                            entity.addPart(
                                    "seller_id",
                                    new StringBody(
                                            CommonUtilities
                                                    .getSellerId(getApplicationContext())));
                            entity.addPart(
                                    "shop_id",
                                    new StringBody(
                                            CommonUtilities
                                                    .getCurentShopId(
                                                            getApplicationContext())
                                                    .toString()));
                            entity.addPart(
                                    "product_pId",
                                    new StringBody(
                                            CommonUtilities
                                                    .getMaxProductId(
                                                            getApplicationContext())
                                                    .toString()));

                            entity.addPart("cat_id", new StringBody(cat_id));


                            entity.addPart("product_image", new StringBody(
                                    allPath[i]));

                            totalSize = entity.getContentLength();

                            httppost.setEntity(entity);


                            // Making server call
                            HttpResponse response = httpclient
                                    .execute(httppost);

                            HttpEntity r_entity = response.getEntity();

                            int statusCode = response.getStatusLine()
                                    .getStatusCode();

                            if (statusCode == 200) {
                                // Server response
                                responseString = EntityUtils
                                        .toString(r_entity);
                            } else {
                                responseString = "Error occurred! Http Status Code: "
                                        + statusCode;
                                flagForError = true;

                            }

                        } catch (ClientProtocolException e) {
                            responseString = e.toString();
                            flagForError = true;

                        } catch (IOException e) {
                            responseString = e.toString();

                            flagForError = true;
                        }

                        if (flagForError == false) {

                            updateAfterResponse(responseString);

                        } else {



                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    // TODO Auto-generated method stub

                                    uploadDialog.cancel();
                                    dialogForError();
                                }
                            });

                            task.cancel(true);
                            break;

                        }
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                progressBar.setProgress(0);
                            }
                        });

                        if (i == allPath.length - 1) {

                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    // TODO Auto-generated method stub
                                    txtCount.setText("Done");
                                }
                            });

                        }
                        // return responseString;
                    }
                    return null;

                }

                @Override
                protected void onPostExecute(Void result) {

                    // showing the server response in an alert
                    // dialog

                    super.onPostExecute(result);

                    // if (i == (allPath.length - 1)) {

                    if (uploadDialog.isShowing()) {

                        uploadDialog.dismiss();
                    }


                }

            }.execute();

and it is giving me error

12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: Process: com.elsner.orderlite, PID: 10320
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime:     at android.os.AsyncTask$3.done(AsyncTask.java:300)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime:     a`enter code here`t java.util.concurrent.FutureTask.run(FutureTask.java:242)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:841)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime:  Caused by: java.lang.NoClassDefFoundError: org.apache.http.Consts
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime:     at org.apache.http.entity.mime.content.StringBody.<init>(StringBody.java:147)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime:     at com.elsner.sellerproduct.CustomGalleryActivity$2$1.doInBackground(CustomGalleryActivity.java:311)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime:     at com.elsner.sellerproduct.CustomGalleryActivity$2$1.doInBackground(CustomGalleryActivity.java:221)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
12-10 17:35:58.710 10320-10340/? E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:841) 

3 Answers 3

3

in bundle.gradle use

compile('org.apache.httpcomponents:httpmime:4.3.6') {
    exclude module: 'httpclient'
}
compile 'org.apache.httpcomponents:httpclient-android:4.3.5'
Sign up to request clarification or add additional context in comments.

Comments

0

To do multipart POST calls, you need to get three additional Apache open source projects: Apache Commons IO, Mime4j, and HttpMime. You can download these projects from the following web sites: Commons IO: http://commons.apache.org/io/ Mime4j: http://james.apache.org/mime4j/ HttpMime: http://hc.apache.org/downloads.cgi (inside of HttpClient)

1 Comment

but httpmime jar file working fine with eclipse. and StringBody class using that org.apache.http.Consts.
0

If you are using sdk version 23, you have to add to your gradle:
android {
compileSdkVersion 23
...
useLibrary 'org.apache.http.legacy'
...
}

1 Comment

useLibrary 'org.apache.http.legacy' dosen't have Classes called, org.apache.http.ContentType,org.apache.http.Consts etc.

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.