Android-Studio无法找到导致java.lang.NoClassDefFoundError的类

时间:2015-07-03 03:08:06

标签: java android

如何解决此错误?我尽我所能,但我仍然得到这个错误

引起:java.lang.NoClassDefFoundError: info.androidhive.camerafileupload.MyMultiPartEntity

我正在使用这些库

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

我从http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/

获取此代码

这是我的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("abc@gmail.com"));

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

    }

这是我的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)

摇篮

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

2 个答案:

答案 0 :(得分:1)

我实际上已经将这个精确的教程移植到Android Studio,并遇到了您所面临的同样问题。

在尝试了很多东西来解决之后,这就是我能够让它发挥作用的方式。

使用以下罐子:

然后,在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'
}

GitHub上的完整项目:

https://github.com/dmnugent80/CameraPhotoUpload

答案 1 :(得分:0)

您可以尝试更改依赖关系。

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

然后,清理并重建项目,然后重试。

在旁注中,compilepackageprovided之间的差异如下。 (引自here

  

提供 - 仅编译时依赖

     

package - 仅限打包时间

     

依赖编译 - 编译时和包时依赖