java.lang.NoSuchFieldError:org.apache.http.message.BasicHeaderValueFormatter.INSTANCE机器人 [英] java.lang.NoSuchFieldError: org.apache.http.message.BasicHeaderValueFormatter.INSTANCE android

查看:979
本文介绍了java.lang.NoSuchFieldError:org.apache.http.message.BasicHeaderValueFormatter.INSTANCE机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用以下code上传大量视频,但得到这个错误< java.lang.NoSuchFieldError:org.apache.http.message.BasicHeaderValueFormatter.INSTANCE > 。我想它的发生是由于多个类路径,但如何在Eclipse中纠正。为此我已经使用的jar文件从 http://hc.apache.org/downloads.cgi 。有只包括HttpClient的-4.4.jar,的HttpCore-4.4.jar ,httpmime-4.4.jar从链路

 公共类MainActivity扩展ActionBarActivity {
    INT SELECT_VIDEO = 1;
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        意向意图=新的意图();
        intent.setType(视频/ *);
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(意向,选择视频),SELECT_VIDEO);
    }


    公共无效onActivityResult(INT申请code,INT结果code,意图数据){
        如果(结果code == RESULT_OK){
            如果(要求code == SELECT_VIDEO){
                的System.out.println(SELECT_VIDEO);
                乌里selectedVideoUri = data.getData();
                字符串selectedPath = getPath(selectedVideoUri);
                的System.out.println(SELECT_VIDEO路径:+ selectedPath);
                尝试 {
                    uploadVideo(selectedPath);
                }赶上(ParseException的| IOException异常E){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                }
            }
        }
    }

    私有String getPath(URI URI){
        的String []投影= {MediaStore.Video.Media.DATA,MediaStore.Video.Media.SIZE,MediaStore.Video.Media.DURATION};
        光标光标= managedQuery(URI,投影,NULL,NULL,NULL);
        cursor.moveToFirst();
        串文件路径= cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
        INT文件大小= cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
        持续时间长= TimeUnit.MILLISECONDS.toSeconds(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)));
         返回文件路径;
    }


    @燮pressWarnings(德precation)
    私人无效uploadVideo(字符串将videoPath)抛出ParseException的,IOException异常{

        HttpClient的HttpClient的=新DefaultHttpClient();
        HttpPost httppost =新HttpPost(URL);

        FileBody filebodyVideo =新FileBody(新文件(将videoPath));
        StringBody标题=新StringBody(文件名:+将videoPath);
        StringBody说明=新StringBody(这是视频的说明);

        MultipartEntity reqEntity =新MultipartEntity();
        reqEntity.addPart(videoFile,filebodyVideo);
        reqEntity.addPart(题,题);
        reqEntity.addPart(说明,说明);
        httppost.setEntity(reqEntity);

        // DEBUG
        的System.out.println(执行请求+ httppost.getRequestLine());
        HTT presponse响应= httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();

        // DEBUG
        的System.out.println(response.getStatusLine());
        如果(resEntity!= NULL){
          的System.out.println(EntityUtils.toString(resEntity));
        } // 万一

        如果(resEntity!= NULL){
          resEntity.consumeContent();
        } // 万一

        httpclient.getConnectionManager().shutdown();
    uploadVideo的} //结束()

}
 

logcat的:

  02-16 14:16:18.421:E / AndroidRuntime(20632):工艺: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):在org.apache.http.entity.ContentType.toString(ContentType.java:153)
。02-16 14:16:18.421:E / AndroidRuntime(20632):在org.apache.http.entity.mime.MultipartFormEntity< INIT>(MultipartFormEntity.java:52)
02-16 14:16:18.421:E / AndroidRuntime(20632):在org.apache.http.entity.mime.MultipartEntityBuilder.buildEntity(MultipartEntityBuilder.java:226)
02-16 14:16:18.421:E / AndroidRuntime(20632):在org.apache.http.entity.mime.MultipartEntity.getEntity(MultipartEntity.java:119)
02-16 14:16:18.421:E / AndroidRuntime(20632):在org.apache.http.entity.mime.MultipartEntity.getContentType(MultipartEntity.java:150)
02-16 14:16:18.421:E / AndroidRuntime(20632):在org.apache.http.impl.client.AbstractHttpClient.isMoMMS(AbstractHttpClient.java:757)
02-16 14:16:18.421:E / AndroidRuntime(20632):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:581)
02-16 14:16:18.421:E / AndroidRuntime(20632):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:511)
02-16 14:16:18.421:E / AndroidRuntime(20632):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:489)
02-16 14:16:18.421:E / AndroidRuntime(20632):在com.example.testingandroid.MainActivity.uploadVideo(MainActivity.java:162)
02-16 14:16:18.421:E / AndroidRuntime(20632):在com.example.testingandroid.MainActivity.onActivityResult(MainActivity.java:113)
02-16 14:16:18.421:E / AndroidRuntime(20632):在android.app.Activity.dispatchActivityResult(Activity.java:5456)
02-16 14:16:18.421:E / AndroidRuntime(20632):在android.app.ActivityThread.deliverResults(ActivityThread.java:3549)
02-16 14:16:18.421:E / AndroidRuntime(20632):在android.app.ActivityThread.handleSendResult(ActivityThread.java:3596)
02-16 14:16:18.421:E / AndroidRuntime(20632):在android.app.ActivityThread.access $ 1300(ActivityThread.java:151)
02-16 14:16:18.421:E / AndroidRuntime(20632):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1369)
02-16 14:16:18.421:E / AndroidRuntime(20632):在android.os.Handler.dispatchMessage(Handler.java:110)
02-16 14:16:18.421:E / AndroidRuntime(20632):在android.os.Looper.loop(Looper.java:193)
02-16 14:16:18.421:E / AndroidRuntime(20632):在android.app.ActivityThread.main(ActivityThread.java:5292)
02-16 14:16:18.421:E / AndroidRuntime(20632):在java.lang.reflect.Method.invokeNative(本机方法)
02-16 14:16:18.421:E / AndroidRuntime(20632):在java.lang.reflect.Method.invoke(Method.java:515)
02-16 14:16:18.421:E / AndroidRuntime(20632):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:828)
02-16 14:16:18.421:E / AndroidRuntime(20632):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
02-16 14:16:18.421:E / AndroidRuntime(20632):在dalvik.system.NativeStart.main(本机方法)
 

解决方案

您code能摆脱掉的德precated code MultipartEntity 和使用 MultipartEntityBuilder 。然而这里的相关的具体问题是,与新加入的那些核心机器人库冲突。现在也有可用的Maven仓库。你可以包括以下code到以下文件:

  

build.grade(模块:应用程序)

 编译('org.apache.httpcomponents:httpmime:4.3.6'){
    排除模块:HttpClient的
}
编译org.apache.httpcomponents:HttpClient的-安卓4.3.5
 

这是pretty的简单得多的方式来工作。由于奥列格的答案<一href="http://stackoverflow.com/questions/27366430/getting-nosuchfielderror-instance-org-apache-http-message-basicheadervalueparser">here

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)

解决方案

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

这篇关于java.lang.NoSuchFieldError:org.apache.http.message.BasicHeaderValueFormatter.INSTANCE机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆