2015-12-10 7 views
2

Я конвертирую свой проект в Studio из eclipse. Я отправляю изображение в многочастном фрагменте с помощью httpmime 4.5.1 jar file. Это мой код.it дает мне ошибку NoClassDefFoundError: org.apache.http.Consts.java.lang.NoClassDefFoundError: org.apache.http.Consts

Пожалуйста, помогите мне.

Это мой код.

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

и он дает мне ошибку

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)  

ответ

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)

+0

но httpmime банку файл работает нормально с затмением. и StringBody, используя этот org.apache.http.Consts. –

0

Если вы используете SDK версии 23, вы должны добавить к вашему Gradle:
андроид {
compileSdkVersion 23
. ..
useLibrary 'org.apache.http.legacy'
...
}

+0

useLibrary 'org.apache.http.legacy' dosen't имеют классы, называемые org.apache.http.ContentType, org.apache.http.Consts и т. Д. –

3

в bundle.gradle использования

compile('org.apache.httpcomponents:httpmime:4.3.6') { 
    exclude module: 'httpclient' 
} 
compile 'org.apache.httpcomponents:httpclient-android:4.3.5' 
Смежные вопросы