java.lang.NoClassDefFoundError:运行时旧手机中的org.apache.http.entity.mime.content.FileBody

时间:2018-02-24 21:58:21

标签: android android-studio apache-httpcomponents

我正在开发一个将图像上传到服务器的应用程序。这在新手机中运行得很好但是当我在较旧的手机中运行时,即使正确添加了所有依赖项,我也会收到此错误:

java.lang.NoClassDefFoundError: org.apache.http.entity.mime.content.FileBody

请注意,不使用多部分帖子的简单消息可以正常运行。

这是手机数据:

APP_VERSION_CODE=51
APP_VERSION_NAME=2.61
ANDROID_VERSION=4.1.2
PHONE_MODEL=GT-I8190N

gradle配置:

android {

compileSdkVersion 23
buildToolsVersion '25.0.0'
defaultConfig {
    minSdkVersion 14
    targetSdkVersion
    multiDexEnabled true
    versionCode 51
    versionName "2.61"
}


...
    useLibrary 'org.apache.http.legacy'
...

}


dependencies {

...

    compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
    compile('org.apache.httpcomponents:httpmime:4.3') {
        exclude module: "httpclient"
    }

}

这是我正在使用的代码:

  HttpClient client = getHTTPClientAC(true);
    HttpPost post = getACPost(url);
    FileBody fb = new FileBody(toUploadFile);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    //

    builder.addPart("file", fb);
    final HttpEntity yourEntity = builder.build();
    post.setEntity(yourEntity);

我尝试过使用mimetypes的旧版本,但这是同样的错误。

1 个答案:

答案 0 :(得分:0)

我修好了。我改变了apache导入:

import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;

对此:

import com.android.internal.http.multipart.FilePart;
import com.android.internal.http.multipart.MultipartEntity;
import com.android.internal.http.multipart.Part;
import com.android.internal.http.multipart.StringPart;

代码改为:

    Part[] parts = new Part[1];
    try {
        parts[0] = new FilePart("file", toUploadFile);
        MultipartEntity multipartEntity = new MultipartEntity(parts);
        post.setEntity(multipartEntity);
    } catch (Exception e) {
        e.printStackTrace();
    }

这适用于新旧手机。我想打一个人......谢谢!

相关问题