如何将curl命令传输到http post请求
问题描述:
我正在使用Facebook对象api将图像上传到facebook分段服务。 API文档是在这里 http://developers.facebook.com/docs/opengraph/using-object-api/如何将curl命令传输到http post请求
在doucement,它使用卷曲上传的图片:
卷曲-X POST https://graph.facebook.com/me/staging_resources -F [email protected]/prawn-curry-1.jpg -F =的access_token $ USER_ACCESS_TOKEN
如何在我的android应用程序中使用http post请求实现相同的功能?
非常感谢!
答
如果你知道如何从Android发布照片,这应该没有什么不同。
Bundle params = new Bundle();
params.putByteArray("file", data);
Request request = new Request(
Session.getActiveSession(),
"me/staging_resources",
params,
HttpMethod.POST
);
Response response = request.executeAndWait();
// handle the response
答
String uri = "https://graph.facebook.com/me/staging_resources";
HttpResponse response = null;
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(uri);
MultipartEntity postEntity = new MultipartEntity();
String picPath = (Uri.parse(picUriStr)).getPath();
File file = new File("/data/local/tmp/images.jpg");
postEntity.addPart("file", new FileBody(file, "image/jpeg"));
postEntity.addPart("access_token", new StringBody("your access token string here"));
post.setEntity(postEntity);
response = client.execute(post);
}
catch (ClientProtocolException e) {
Log.d(TAG, "exception");
e.printStackTrace();
}
catch (IOException e) {
Log.d(TAG, "exception");
e.printStackTrace();
}
HttpEntity responseEntity = response.getEntity();
if (responseEntity== null) {
Log.d(TAG, "responseEntity is null");
return "";
}
+1
这里是我使它的工作代码。 – jiawen 2013-05-06 01:36:21
所以我想这里的数据是字节数组从文件读入。如果我可以直接使用文件路径会更好。我使用了MultipartEntity,它工作。 – jiawen 2013-05-06 01:24:53
这将是很好,如果我们可以使用请求在Facebook的SDK与文件路径 – jiawen 2013-05-06 01:37:45