2016-09-21 19 views
0

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) 

方法

/************************************ 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') 
} 
+0

スタックトレースに表示されている行は何ですか? –

+0

TNX。これは、 'FileBody fileBody = new FileBody(image); –

+0

と次の行' 'zupportdesk.desk.zupport.chatsystem.Chatting.multiPartPost(Chatting.java:247) のzupportdesk.desk.zupport.chatsystemの行です。 Chatting.onActivityResult(Chatting.java:223) ' –

答えて

1

私も数日戻って同じ問題を抱えて。

okhttpクライアントを使用して解決します。あなたはokHttpクライアントを試すことができます

...あなたのGradleにこの行を追加すること

Uploading a large file in multipart using OkHttp

0

してみてください。

compile 'org.apache.httpcomponents:httpmime:4.3.5' 
関連する問題