我需要将照片从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);
为了构建Uri对象,“ setMethod”和“ setParameters”是自己的方法。外部api很好地接受了参数,但没有照片。期望照片在HttpBody字段中。
任何想法?谢谢
谢谢
最佳答案
问题可能是内容类型中的分号:
fileentity = new FileEntity(file,"image/jpeg;");
^