不推荐使用 MultipartEntity 类型 [英] The type MultipartEntity is deprecated

查看:31
本文介绍了不推荐使用 MultipartEntity 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档说 org.apache.http.entity.mime.MultipartEntity 类已弃用.有人可以给我建议一个替代方案吗?

我在我的代码中使用这个:

entity.addPart("params", new StringBody("{\"auth\":{\"key\":\""+ authKey + "\"},\"template_id\":\"" + templateId + "\"}"));entity.addPart("my_file", new FileBody(image));httppost.setEntity(实体);

解决方案

如果你仔细阅读文档,你会注意到你应该使用 MultipartEntityBuilder 作为替代.

例如:

MultipartEntityBuilder builder = MultipartEntityBuilder.create();/* 设置 HttpMultipartMode 的示例 */builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);/* 添加图像部分的示例 */FileBody fileBody = new FileBody(new File(image));//图像应该是一个字符串builder.addPart("my_file", fileBody);//等等

请注意,FileBody 类,通过它可以提供mimeType内容类型等>

完成将构建指令传递给构建器后,您可以获得构建的HttpEntity 通过调用 MultipartEntityBuilder#build() 方法:

HttpEntity entity = builder.build();

The documentation says the org.apache.http.entity.mime.MultipartEntity class is deprecated. Could anybody please suggest me an alternative ?

I am using this in my code like this:

entity.addPart("params", new StringBody("{\"auth\":{\"key\":\""
            + authKey + "\"},\"template_id\":\"" + templateId + "\"}"));
entity.addPart("my_file", new FileBody(image));
httppost.setEntity(entity);

解决方案

If you read the docs carefully, you'll notice that you should use MultipartEntityBuilder as an alternative.

For example:

MultipartEntityBuilder builder = MultipartEntityBuilder.create();        

/* example for setting a HttpMultipartMode */
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

/* example for adding an image part */
FileBody fileBody = new FileBody(new File(image)); //image should be a String
builder.addPart("my_file", fileBody); 
//and so on

Note that there are several constructors for the FileBody class, by which you can provide mimeType, content type, etc.

After you're done with passing build instructions to the builder, you can get the built HttpEntity by invoking the MultipartEntityBuilder#build() method:

HttpEntity entity = builder.build();

这篇关于不推荐使用 MultipartEntity 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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