2

我正在使用 facebook 对象 api 将图像上传到 facebook 登台服务。api 文件在这里 http://developers.facebook.com/docs/opengraph/using-object-api/

在文档中,它使用 curl 上传图像:

curl -X POST https://graph.facebook.com/me/staging_resources -F file=@images/prawn-curry-1.jpg -F access_token=$USER_ACCESS_TOKEN

如何在我的 android 应用程序中使用 http post 请求实现相同的功能?

非常感谢!

4

2 回答 2

2
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 "";
}
于 2013-05-06T01:35:54.117 回答
0

如果您知道如何从 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
于 2013-05-04T17:29:29.033 回答