HTTP multipart classes in java

2019-05-26 09:45发布

I'm trying to follow the answer to THAT question

But it seems that classes are not included with the default Android package since for that code:

File file = new File("FileToSend.txt");
HttpClient client = new HttpClient();

String url = "http://www.yourdomain.com/destination.php";
PostMethod postMethod = new PostMethod(url);

Part[] parts = {new FilePart(file.getName(), file)};
postMethod.setParameter("name", "value"); // set parameters like this instead in separate call

postMethod.setRequestEntity( new MultipartRequestEntity(parts, postMethod.getParams()));

int status = client.executeMethod(postMethod);

I have the following errors:

Cannot instantiate the type HttpClient
FilePart cannot be resolved to a type XXXXX.java
MultipartRequestEntity cannot be resolved to a type XXXXX.java
Part cannot be resolved to a type XXXXX.java
PostMethod cannot be resolved to a type XXXXX.java

How can I solve those errors, is there some library I must add? if yes please let me know how can I download it.

标签: java http
1条回答
\"骚年 ilove
2楼-- · 2019-05-26 10:30

HttpClient is (now) an interface. Use DefaultHttpClient instead. Here's replacement classes for some of the others you listed:

FilePart - FileBody
MultipartRequestEntity - MultipartEntity
Part - ContentBody
PostMethod - HttpPost
查看更多
登录 后发表回答