1

How to fix this error? I try everything i can but i am still getting this error

Caused by: java.lang.NoClassDefFoundError: info.androidhive.camerafileupload.MyMultiPartEntity

I am using these libraries

  • httpclient-4.2.5.jar,
  • httpcore-4.2.4.jar,
  • httpmime-4.2.5.jar

I get this code from http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/

This is my AsyncTask:

private class UploadFileToServer extends AsyncTask<Void, Integer, String> {
        @Override
        protected void onPreExecute() {
            // setting progress bar to zero
            progressBar.setProgress(0);
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Integer... progress) {
            // Making progress bar visible
            progressBar.setVisibility(View.VISIBLE);

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

            // updating percentage value
            txtPercentage.setText(String.valueOf(progress[0]) + "%");
        }

        @Override
        protected String doInBackground(Void... params) {
            return uploadFile();
        }

        private String uploadFile() {
            String responseString = null;

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

            try {
                MyMultiPartEntity entity = new MyMultiPartEntity( // Casuing NoClassDefFoundError
                        new ProgressListener() {
                            @Override
                            public void transferred(long num) {
                                publishProgress((int) ((num / (float) totalSize) * 100));
                            }
                        });

                File sourceFile = new File(filePath);

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

                // Extra parameters if you want to pass to server
                entity.addPart("website",
                        new StringBody("www.androidhive.info"));
                entity.addPart("email", new StringBody("[email protected]"));

                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;
                }

            } catch (ClientProtocolException e) {
                responseString = e.toString();
            } catch (IOException e) {
                responseString = e.toString();
            }/* catch(NoClassDefFoundError e){
                responseString = e.toString();
            }*/

            return responseString;

        }

        @Override
        protected void onPostExecute(String result) {
            Log.e(TAG, "Response from server: " + result);

            // showing the server response in an alert dialog
            showAlert(result);

            super.onPostExecute(result);
        }

    }

This is my Multipart Entity

public class MyMultiPartEntity extends MultipartEntity {

    private final ProgressListener listener;

    public MyMultiPartEntity(final ProgressListener listener) {
        super();
        this.listener = listener;
    }

    public MyMultiPartEntity(final HttpMultipartMode mode,
                             final ProgressListener listener) {
        super(mode);
        this.listener = listener;
    }

    public MyMultiPartEntity(HttpMultipartMode mode, final String boundary,
                             final Charset charset, final ProgressListener listener) {
        super(mode, boundary, charset);
        this.listener = listener;
    }

    @Override
    public void writeTo(final OutputStream outStream) throws IOException {
        super.writeTo(new CountingOutputStream(outStream, this.listener));
    }

    public interface ProgressListener {
        void transferred(long num);
    }

    public static class CountingOutputStream extends FilterOutputStream {

        private final ProgressListener listener;
        private long transferred;

        public CountingOutputStream(final OutputStream out,
                final ProgressListener listener) {
            super(out);
            this.listener = listener;
            this.transferred = 0;
        }

        public void write(byte[] b, int off, int len) throws IOException {
            out.write(b, off, len);
            this.transferred += len;
            this.listener.transferred(this.transferred);
        }

        public void write(int b) throws IOException {
            out.write(b);
            this.transferred++;
            this.listener.transferred(this.transferred);
        }
    }
}

Logcat:

07-03 13:56:05.128  32464-32464/info.androidhive.camerafileupload E/dalvikvm﹕ Could not find class 'info.androidhive.camerafileupload.MyMultiPartEntity', referenced from method info.androidhive.camerafileupload.UploadActivity$UploadFileToServer.uploadFile
07-03 13:56:05.158  32464-32549/info.androidhive.camerafileupload E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: info.androidhive.camerafileupload, PID: 32464
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.NoClassDefFoundError: info.androidhive.camerafileupload.MyMultiPartEntity
            at info.androidhive.camerafileupload.UploadActivity$UploadFileToServer.uploadFile(UploadActivity.java:151)
            at info.androidhive.camerafileupload.UploadActivity$UploadFileToServer.doInBackground(UploadActivity.java:141)
            at info.androidhive.camerafileupload.UploadActivity$UploadFileToServer.doInBackground(UploadActivity.java:119)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "22.0.1"
    defaultConfig {
        applicationId "info.androidhive.camerafileupload"
        minSdkVersion 11
        targetSdkVersion 21
    }
    buildTypes {
        release {
            minifyEnabled false;
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    productFlavors {
    }
}

dependencies {
    provided files('libs/httpclient-4.2.5.jar')
    provided files('libs/httpcore-4.2.4.jar')
    provided files('libs/httpmime-4.2.5.jar')
    provided files('libs/android-support-v4.jar')
    compile 'com.android.support:appcompat-v7:22.1.1'
}
1
  • Can you post your build.gradle file under the app folder as well ? Commented Jul 3, 2015 at 3:14

2 Answers 2

1

I actually ported this exact tutorial to Android Studio a while back, and ran into the same exact issue that you're facing.

After trying many things to fix it, this is how I was able to get it working.

Use the following jars :

Then, in your build.gradle:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'

    compile('org.apache.httpcomponents:httpmime:4.3.5') {
        exclude module: 'httpclient'
    }
    compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
}

Full project on GitHub:

https://github.com/dmnugent80/CameraPhotoUpload

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

2 Comments

I try your code Daniel, your the best you save my day thank you so much
Its working now, i finally apply this code to my project thanks again
0

You can try changing your dependencies as follow.

dependencies {
    compile files('libs/httpclient-4.2.5.jar')
    compile files('libs/httpcore-4.2.4.jar')
    compile files('libs/httpmime-4.2.5.jar')
    compile files('libs/android-support-v4.jar')
    compile 'com.android.support:appcompat-v7:22.1.1'
}

Then, clean and rebuild the project and try again.

On the side note, the difference between compile, package and provided are as followed. (referenced from here)

provided - compile-time only dependency

package - package-time only

dependency compile - compile-time and package-time dependency

4 Comments

I 've got Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.7.0_79\bin\java.exe'' finished with non-zero exit value 2
When i run the project
What's your Android Studio, Gradle and Java versions ?
Android Studio 1.2.1.1 and i have two installed on my machine Java 1.8 and Java 1.7

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.