0

Hi am trying to upload files using multipart/form-data.

Process: zupportdesk.desk.zupport.chatsystem, PID: 31766
java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType
at org.apache.http.entity.mime.content.FileBody.<init>(FileBody.java:89)
at zupportdesk.desk.zupport.chatsystem.Chatting.multiPartPost(Chatting.java:247)
at zupportdesk.desk.zupport.chatsystem.Chatting.onActivityResult(Chatting.java:223)
at android.app.Activity.dispatchActivityResult(Activity.java:5493)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3522)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3569)
at android.app.ActivityThread.access$1300(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5433)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)

Methods

/************************************ Image Selecting ******************************************/
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1)
            if (resultCode == Chatting.RESULT_OK) {
                Uri selectedImage = data.getData();

                String filePath = getPath(selectedImage);
                Log.d("chatting_imagePath2", filePath);
                String file_extn = filePath.substring(filePath.lastIndexOf(".") + 1);

                if (file_extn.equals("img") || file_extn.equals("jpg") || file_extn.equals("jpeg") || file_extn.equals("gif") || file_extn.equals("png")) {
                    //FINE

                    try {
                        multiPartPost(filePath);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    //NOT IN REQUIRED FORMAT
                }
            }
    }

    public String getPath(Uri uri) {
        String[] projection = {MediaStore.MediaColumns.DATA};
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
        cursor.moveToFirst();
        String imagePath = cursor.getString(column_index);
        Log.d("chatting_imagePath", imagePath);
        return cursor.getString(column_index);
    }


    public void multiPartPost(String filePath) throws ClientProtocolException, IOException {
        File image = new File(filePath);
        FileBody fileBody = new FileBody(image);

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("https:// MYURL /api/chat/UploadFileByOperator");
        post.setHeader("enctype", "multipart/form-data");

        MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
        multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        multipartEntity.addPart("sampleImage", fileBody);
        post.setEntity(multipartEntity.build());

        HttpResponse response = client.execute(post);
        String responseBody = EntityUtils.toString(response.getEntity());
        Log.d("Post_image", responseBody);
    }

    /**********************************************************************************************/

Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"
    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        applicationId "zupportdesk.desk.zupport.chatsystem"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }




    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude '...'
    }

}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile files('libs/gson-2.2.2.jar')
    compile files('libs/signalr-client-sdk-android.jar')
    compile files('libs/signalr-client-sdk.jar')
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.android.support:recyclerview-v7:24.2.0'
    compile 'com.android.support:design:24.2.0'
    compile 'com.android.support:cardview-v7:24.2.0'
    compile 'br.com.liveo:navigationdrawer-material:2.5.1'
    compile 'org.java-websocket:Java-WebSocket:1.3.0'
    compile 'com.google.android.gms:play-services:7.5.0'
    compile files('libs/httpmime-4.3.1.jar')
}
3
  • what is on lines indicated in stacktrace? Commented Sep 21, 2016 at 9:42
  • TNX. This is the line FileBody fileBody = new FileBody(image); Commented Sep 21, 2016 at 9:45
  • and on next line at zupportdesk.desk.zupport.chatsystem.Chatting.multiPartPost(Chatting.java:247) at zupportdesk.desk.zupport.chatsystem.Chatting.onActivityResult(Chatting.java:223) Commented Sep 21, 2016 at 9:47

2 Answers 2

1

I also got same problem few days back.

Solve by using okHttp Client.

You can try okHttp Client..

Uploading a large file in multipart using OkHttp

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

Comments

0

Try adding this line in your gradle.

compile 'org.apache.httpcomponents:httpmime:4.3.5'

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.