3

trying to upload large video using below code but getting this error<java.lang.NoSuchFieldError: org.apache.http.message.BasicHeaderValueFormatter.INSTANCE>. i guess its happening due to multiple classpath but how to rectify in eclipse. and for this i have used jar files from http://hc.apache.org/downloads.cgi.have included only httpclient-4.4.jar, httpcore-4.4.jar, httpmime-4.4.jar from the link.

public class MainActivity extends ActionBarActivity {
    int SELECT_VIDEO = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);     

        Intent intent = new Intent();
        intent.setType("video/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent,"Select a Video "), SELECT_VIDEO);       
    }


    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_VIDEO) {
                System.out.println("SELECT_VIDEO");
                Uri selectedVideoUri = data.getData();
                String selectedPath = getPath(selectedVideoUri);
                System.out.println("SELECT_VIDEO Path : " + selectedPath);
                try {
                    uploadVideo(selectedPath);
                } catch (ParseException | IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }      
        }
    }

    private String getPath(Uri uri) {
        String[] projection = { MediaStore.Video.Media.DATA, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION}; 
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        cursor.moveToFirst(); 
        String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
        int fileSize = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
        long duration = TimeUnit.MILLISECONDS.toSeconds(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)));
         return filePath;
    }


    @SuppressWarnings("deprecation")
    private void uploadVideo(String videoPath) throws ParseException, IOException {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("URL");

        FileBody filebodyVideo = new FileBody(new File(videoPath));
        StringBody title = new StringBody("Filename: " + videoPath);
        StringBody description = new StringBody("This is a description of the video");

        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("videoFile", filebodyVideo);
        reqEntity.addPart("title", title);
        reqEntity.addPart("description", description);
        httppost.setEntity(reqEntity);

        // DEBUG
        System.out.println( "executing request " + httppost.getRequestLine( ) );
        HttpResponse response = httpclient.execute( httppost );
        HttpEntity resEntity = response.getEntity( );

        // DEBUG
        System.out.println( response.getStatusLine( ) );
        if (resEntity != null) {
          System.out.println( EntityUtils.toString( resEntity ) );
        } // end if

        if (resEntity != null) {
          resEntity.consumeContent( );
        } // end if

        httpclient.getConnectionManager( ).shutdown( );
    } // end of uploadVideo( )

}

Logcat:

02-16 14:16:18.421: E/AndroidRuntime(20632): Process: com.example.testingandroid, PID: 20632
02-16 14:16:18.421: E/AndroidRuntime(20632): java.lang.NoSuchFieldError: org.apache.http.message.BasicHeaderValueFormatter.INSTANCE
02-16 14:16:18.421: E/AndroidRuntime(20632):    at org.apache.http.entity.ContentType.toString(ContentType.java:153)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at org.apache.http.entity.mime.MultipartFormEntity.<init>(MultipartFormEntity.java:52)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at org.apache.http.entity.mime.MultipartEntityBuilder.buildEntity(MultipartEntityBuilder.java:226)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at org.apache.http.entity.mime.MultipartEntity.getEntity(MultipartEntity.java:119)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at org.apache.http.entity.mime.MultipartEntity.getContentType(MultipartEntity.java:150)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at org.apache.http.impl.client.AbstractHttpClient.isMoMMS(AbstractHttpClient.java:757)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:581)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:511)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:489)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at com.example.testingandroid.MainActivity.uploadVideo(MainActivity.java:162)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at com.example.testingandroid.MainActivity.onActivityResult(MainActivity.java:113)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at android.app.Activity.dispatchActivityResult(Activity.java:5456)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3549)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3596)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at android.app.ActivityThread.access$1300(ActivityThread.java:151)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1369)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at android.os.Handler.dispatchMessage(Handler.java:110)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at android.os.Looper.loop(Looper.java:193)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at android.app.ActivityThread.main(ActivityThread.java:5292)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at java.lang.reflect.Method.invokeNative(Native Method)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at java.lang.reflect.Method.invoke(Method.java:515)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
02-16 14:16:18.421: E/AndroidRuntime(20632):    at dalvik.system.NativeStart.main(Native Method)
10
  • asked already multiple times ... android framework already has (older) version of apache httpclient ... you cannot use those libraries as is ... you have to repackage em Commented Feb 16, 2015 at 9:28
  • i did check but couldnt find relavent solution so can u please be more clear? Commented Feb 16, 2015 at 9:29
  • how to repackage them? u mean rename the files? Commented Feb 16, 2015 at 9:30
  • stackoverflow.com/questions/27366430/… this solution is given for android studio and most of the solution are given for android studio only. Commented Feb 16, 2015 at 9:33
  • FSM save us!... do some research ... all you need is to find the jar ... search.maven.org/#search%7Cga%7C1%7Chttpclient-android Commented Feb 16, 2015 at 9:35

1 Answer 1

8

Your code could get rid off the deprecated code of MultipartEntityand use MultipartEntityBuilder. However the specific problem related here is that the core Android libraries conflict with the newly added ones. Also now there are maven repositories available. You could include the following code into the below file:

build.grade (Module:app)

compile('org.apache.httpcomponents:httpmime:4.3.6') {
    exclude module: 'httpclient'
}
compile 'org.apache.httpcomponents:httpclient-android:4.3.5'

It is pretty much simple to work the way around. Thanks to oleg for the answer here

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

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.