1
我需要将存储在SDCARD中的文件的照片发送到外部Api。为了做到这一点,我使用下面的代码:Android使用HttpPost/HttpGet发送照片
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字段中的照片。
有什么想法?由于
感谢
所以你要发送的JPEG的身体,但服务器是说,它没有看到JPEG在身体里? – 2010-06-28 16:05:13
服务器以这种方式使用php拍摄图像 if($ im_s = file_get_contents('php:// input')){ – xger86x 2010-06-28 16:38:20