【问题标题】:how to transfer the curl command to http post request如何将 curl 命令传输到 http post 请求
【发布时间】:2013-04-28 20:47:49
【问题描述】:

我正在使用 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 请求实现相同的功能?

非常感谢!

【问题讨论】:

    标签: android facebook http curl


    【解决方案1】:
    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 "";
    }
    

    【讨论】:

      【解决方案2】:

      如果您知道如何从 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
      

      【讨论】:

      • 所以我猜这里的数据是从文件中读取的字节数组。如果我可以直接使用文件路径会更好。我使用了 MultipartEntity,它可以工作。
      • 如果我们可以在 facebook sdk 中使用带有文件路径的请求会很好
      猜你喜欢
      • 1970-01-01
      • 2016-08-04
      • 2015-05-13
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多