Joo*_*lah 7 java apache post android http
我正在尝试将图像发送到我的API.但是在使用MultipartEntity StringBody时我得到错误,因为不推荐使用StringBody(String).
我不工作.我是Android的一个示例,通过MultipartEntity将图像发送到服务器 - 设置内容类型?.
这是代码:
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();
}
Run Code Online (Sandbox Code Playgroud)
Tho*_*ben 16
不建议使用用于创建新实例的构造函数StringBody.
代替
new StringBody(mApiKey);
Run Code Online (Sandbox Code Playgroud)
你应该使用一个StringBody构造器是第二个参数ContentType一样
new StringBody(mApiKey, ContentType.TEXT_PLAIN);
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请查看:
和
http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/entity/ContentType.html
| 归档时间: |
|
| 查看次数: |
9009 次 |
| 最近记录: |