android - java.lang.NoSuchFieldError : org. apache.http.message.BasicHeaderValueFormatter.INSTANCE android

标签 android eclipse apache httpclient

尝试使用以下代码上传大型视频但出现此错误<java.lang.NoSuchFieldError: org.apache.http.message.BasicHeaderValueFormatter.INSTANCE>。我猜它的发生是由于多个类路径,但如何在 Eclipse 中纠正。为此,我使用了来自 http://hc.apache.org/downloads.cgi 的 jar 文件。仅包含 httpclient-4.4.jar、httpcore-4.4.jar、httpmime-4.4.jar来自链接。

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

}

日志:

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)

最佳答案

您的代码可以摆脱 MultipartEntity 的弃用代码并使用 MultipartEntityBuilder。但是这里涉及的具体问题是核心Android库与新添加的库冲突。现在也有可用的 Maven 存储库。您可以将以下代码包含到以下文件中:

build.grade (Module:app)

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

解决方法非常简单。感谢oleg寻找答案 here

关于android - java.lang.NoSuchFieldError : org. apache.http.message.BasicHeaderValueFormatter.INSTANCE android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28538078/

相关文章:

java - 线程池如何处理 Web 服务器中的静态和动态请求?

python - 让 Apache cgi 在 Python 中发送 json

java - Fragment的onDestroy()中是否需要将ViewBinding设置为null?

android - 超出最小未出列缓冲区计数

java - 手机不会播放点击声

java - Spring-MVC/Hibernate/MySql 的良好开发设置

java - contextMenu 中所选项目的 id 始终为 0

java - 将方法从库覆盖到不同的项目

java - 如何显示自定义列名 JTable 而不是来自数据库?

php - 从 PHP 使用 gnupg 时出现问题(Apache 失败,CLI OK)