我需要将存储在 SDCARD 中的文件中的照片发送到外部 Api。为此,我使用了以下代码:
String responseStr = null;
this.setMethod(request);
this.setParameters(tags, parameters, optional);
String requested = mUri.build().toString();
HttpHost host = new HttpHost(API_REST_HOST, -1,
HttpHost.DEFAULT_SCHEME_NAME);
Log.d(TAG, requested);
try {
HttpPost post = new HttpPost(requested);
File file = new File(filepath);
FileEntity fileentity;
if (filepath.substring(filepath.length()-3, filepath.length
()).equalsIgnoreCase("jpg")) {
fileentity = new FileEntity(file,"image/jpeg;");
fileentity.setChunked(true);
post.setEntity(fileentity);
}
post.addHeader("Content-Type", "image/jpeg;");
HttpResponse response = mClient.execute(host,post);
“setMethod”和“setParameters”是构建 Uri 对象的自有方法。外部 api 接受参数但不接受照片。它期待 HttpBody 字段中的照片。
有什么想法吗?谢谢
谢谢
最佳答案
问题可能是内容类型中的分号:
fileentity = new FileEntity(file,"image/jpeg;");
^
关于Android 使用 HttpPost/HttpGet 发送照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3132104/