0

Hey guys i'm new to android app development, i'm trying to upload file to server using AsyncTask, but when i click a upload button app get crashed, please help with this.(Sorry if there is any language mistake).

UploadFileToServer class

/**
 * Uploading the file to server
 * */
private class UploadFileToServer extends AsyncTask<Void, Integer, String> {
    @Override
    protected void onPreExecute() {
        progressBar.setVisibility(View.VISIBLE);
        super.onPreExecute();
    }

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

    @SuppressWarnings("deprecation")
    private String uploadFile() {
        String responseString;

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

       try {
            AndroidMultiPartEntity entity = new AndroidMultiPartEntity(


                    new AndroidMultiPartEntity.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));//new FileBody(sourceFile)

            // Extra parameters pass to server

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

        return responseString;

    }

logcat error:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
#1. E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
#2.Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType
E/AndroidRuntime:     at org.apache.http.entity.mime.content.FileBody.<init>(FileBody.java:89)

Complete error window as follows:

10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime:     at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:856)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime:  Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime:     at org.apache.http.entity.mime.content.FileBody.<init>(FileBody.java:89)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime:     at com.appnings.cameratrial.UploadActivity$UploadFileToServer.uploadFile(UploadActivity.java:166)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime:     at com.appnings.cameratrial.UploadActivity$UploadFileToServer.doInBackground(UploadActivity.java:139)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime:     at com.appnings.cameratrial.UploadActivity$UploadFileToServer.doInBackground(UploadActivity.java:114)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
10-18 20:51:23.109 14829-15007/? E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:856) 
10-18 20:51:23.440 569-649/? E/android.os.Debug: !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error
Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107
Shivaraj
  • 1
  • 1
  • 5
  • a plethora of things could go wrong with asynctasks. Without a stacktrace, it's hard to figure out what's going on – victorantunes Oct 18 '15 at 14:25
  • Thanks for the responses. i'm happy to be here in stackoverflow.. i updated the logcat error messages.. please help with this. thank you all. – Shivaraj Oct 18 '15 at 15:29
  • Check your libraries. Seem some paths of classes are bad. Maybe you didn't include Apache library. Also read this http://stackoverflow.com/questions/17973970/how-to-solve-java-lang-noclassdeffounderror . Also look at this http://enoent.fr/blog/2015/10/01/use-apache-http-client-on-android-sdk-23/. – Volodymyr Oct 18 '15 at 15:44

3 Answers3

0

Given you haven't posted the error log (from logcat) it would be hard to guess.

Common thing you might have missed:

<uses-permission android:name="android.permission.INTERNET" /> 

outside the application tag in the Manifest.xml

  • hello mr.Evgeny Maltsev, thanks a lot. i added internet permission.. now i updated the question with logcat error. please help me with this. once again thank u – Shivaraj Oct 18 '15 at 15:30
  • No problem :)! As @Vladimir S had mentioned, you might have a dependency issue. If you use AndroidStudio you can try adding the following: android { useLibrary 'org.apache.http.legacy' } in your build.gradle and rebuild. – Evgeny Maltsev Oct 18 '15 at 16:58
  • I added all the library files and dependency still its not working – Shivaraj Oct 19 '15 at 03:53
0

Tried importing different Apache lib files, some classes missing in some lib files finally after couple of imports it worked. thank you all. happy coding.

Shivaraj
  • 1
  • 1
  • 5
0

I solved the same problem

1.Use the right way to add jars.

Right click your lib folder and choice "add library", and input to project.

  1. In settings.gradle, add it.

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


  • Compile environment:
    • Android Studio 1.4
    • target sdk:21
    • jars:httpclient-4.3.6.jar,httpcore-4.3.3.jar, httpmime-4.3.6.jar
Oases Ong
  • 21
  • 3