multipartEntityBuilder

multipartEntityBuilder

我想上传图片并发送数据到服务器。我正在使用multipartentitybuilder进行此操作。我在android工作室上编程。
这是我的密码

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(config.api_url+"profile.php");

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

if(mLastTakenImageAsJPEGFile!=null) {
    builder.addBinaryBody("image", mLastTakenImageAsJPEGFile);
}

builder.addTextBody("token", _appPrefs.getToken());
final HttpEntity reqEntity = builder.build();

httpPost.setEntity(reqEntity);

我得到运行时错误
java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType
at
org.apache.http.entity.mime.MultipartEntityBuilder.addBinaryBody(MultipartEntityBuilder.java:146)

如何解决这个问题?

最佳答案

我发现了httpime4.3.6的这个问题,我还没有找到解决方案,我建议您使用httpime4.2.1,它工作得很好。

MultipartEntity multipartEntity = new MultipartEntity();
ContentBody contentBody = new FileBody(file);
multipartEntity.addPart("image", new FileBody(file));
httpPost.setEntity(multipartEntity);

09-27 11:57