创建默认httpsClient会导致多部分请求中出错

时间:2014-08-07 09:26:37

标签: java android httpclient multipart

我正在尝试使用以下函数执行多部分请求,但是当我创建httpClient时,我得到了这个异常:

  

E / AndroidRuntime(8909):引起:java.lang.NoSuchFieldError:   org.apache.http.message.BasicLineFormatter.INSTANCE

我正在使用 httpclient-4.3.4 (导入也在下面)

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;


public static void uploadFile() throws ClientProtocolException, IOException {
    String filepath = Environment.getExternalStorageDirectory() + "/Download/img.jpg";
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost uploadFile = new HttpPost("ip/myRestservice");

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("field1", "yes", ContentType.TEXT_PLAIN);
File file = new File(filepath);
builder.addBinaryBody("file", file, ContentType.APPLICATION_OCTET_STREAM, "file.ext");
HttpEntity multipart = builder.build();

uploadFile.setEntity(multipart);
CloseableHttpResponse response = httpClient.execute(uploadFile);
HttpEntity responseEntity = response.getEntity();
}

如何解决?

0 个答案:

没有答案