类型MultipartEntity是德precated [英] The type MultipartEntity is deprecated
问题描述
文档说的<一个href=\"http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntity.html\"><$c$c>org.apache.http.entity.mime.MultipartEntity$c$c>该类德precated。有谁请建议我的方法吗?
我在我的code像这样使用的:
entity.addPart(PARAMS,新StringBody({\\身份验证\\:{\\键\\:\\
+ AUTHKEY +\\} \\template_id \\:\\+ templateId +\\}));
entity.addPart(my_file,新FileBody(图片));
httppost.setEntity(实体);
如果你仔细阅读文档,你会发现,你应该使用<一个href=\"http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntityBuilder.html\"><$c$c>MultipartEntityBuilder$c$c>作为替代。
例如:
MultipartEntityBuilder建设者= MultipartEntityBuilder.create();/ *示例设置HttpMultipartMode * /
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);/ *例如添加图像部分* /
FileBody fileBody =新FileBody(新文件(图片)); //形象应该是一个String
builder.addPart(my_file,fileBody);
//等等
请注意,有几个构造函数的<一个href=\"http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/FileBody.html\"><$c$c>FileBody$c$c>类,通过它可以提供的 mime类型的内容类型的等。
你与传递结束后的打造说明应用于生成器,你可以得到内置<一个href=\"http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpEntity.html?is-external=true\"><$c$c>HttpEntity$c$c>通过调用<一个href=\"http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntityBuilder.html\"><$c$c>MultipartEntityBuilder#build()$c$c>方法:
HttpEntity实体= 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是德precated的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!