gpt4 book ai didi

java - Android StringBody(String) 已弃用

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:12:38 27 4
gpt4 key购买 nike

我正在尝试向我的 API 发送图像。但是在使用 MultipartEntity StringBody 时出现错误,因为不推荐使用 StringBody(String)。

我没有工作。我在 Android sending image to Server via MultipartEntity - setting Content-type? 做了一个 sample .

这是代码:

try {

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("api_key", api_key));
params.add(new BasicNameValuePair("access_token", access_token));

API api = new API(mApiKey, mApiSecret);


HttpClient client = new DefaultHttpClient();
HttpPost postMethod = new HttpPost("MY API URL");
File file = new File(imagePath);
MultipartEntity entity = new MultipartEntity();
FileBody contentFile = new FileBody(file);

StringBody api_key = new StringBody(mApiKey);
StringBody access_token = new StringBody(access_token);

entity.addPart("api_key", api_key);
entity.addPart("hash", access_token);
entity.addPart("image", contentFile);

postMethod.setEntity(entity);
client.execute(postMethod);



} catch (Exception e) {
e.printStackTrace();
}

最佳答案

用于创建 StringBody 新实例的构造函数已弃用。

代替

new StringBody(mApiKey);

你应该使用一个 StringBody 构造函数,第二个参数是一个 ContentType 就像

new StringBody(mApiKey, ContentType.TEXT_PLAIN);

更多信息请看:

http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/StringBody.html

http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/entity/ContentType.html

关于java - Android StringBody(String) 已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27310992/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com