【问题标题】:Post images from android applications to facebook applications-wall将图像从 android 应用程序发布到 facebook 应用程序墙
【发布时间】:2014-02-15 04:45:43
【问题描述】:

我正在尝试将用户使用我的 Android 应用程序创建的图像发布到 Facebook 上的应用程序墙。我已经知道如何在用户的墙上发布图像。

我找到了如何使用 php 进行操作的教程:
http://jorgealbaladejo.com/2011/06/13/publish-to-facebook-page-or-applications-wall-with-php/

但我不知道如何获取 page_id 或如何使用适用于 Android 的 Facebook sdk。


编辑

我在 Facebook 墙上发帖的代码是:

public void postImageonWall(String path) {

    byte[] data = null;

    Bitmap bi = BitmapFactory.decodeFile(path);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    data = baos.toByteArray();


    Facebook facebook = ((MainActivity) appcontext).getFacebook();

    Log.d("Facebook-PostImage","facebook: " + facebook);
    Bundle params = new Bundle();
    params.putString(Facebook.TOKEN, facebook.getAccessToken());
    params.putString("method", "photos.upload");
    params.putString("message", "Message Text");
    params.putString("link","ANDROID_MARKET_LINK"); //or any other link
    params.putString("name", "APP/GAME NAME");
    params.putByteArray("picture", data);


    Log.d("Facebook-PostImage","AsyncRunner going to be called");
    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
    mAsyncRunner.request(null, params, "POST", new RequestListener() {
        public void onComplete(final String response, final Object state) {
                try {
                    // process the response here: (executed in background thread)
                    Log.d("Facebook-Example", "Response: " + response.toString());
                    JSONObject json = Util.parseJson(response);
                    final String src = json.getString("src");

                    // then post the processed result back to the UI thread
                    // if we do not do this, an runtime exception will be generated
                    // e.g. "CalledFromWrongThreadException: Only the original
                    // thread that created a view hierarchy can touch its views."

                } catch (JSONException e) {
                    Log.w("Facebook-Example", "JSON Error in response");
                } catch (FacebookError e) {
                    Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
                }
            }
        @Override
        public void onFacebookError(FacebookError e, Object state) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onIOException(IOException e, Object state) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onFileNotFoundException(FileNotFoundException e,
                Object state) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onMalformedURLException(MalformedURLException e,
                    Object state) {
                // TODO Auto-generated method stub
            }
          }, null);
}

【问题讨论】:

  • 您不确定如何获取页面的 page_id ?在此处查看几个示例 - stackoverflow.com/questions/10149209/…。获得 page_id 后,您可以通过 - developers.facebook.com/docs/reference/api/page/#photos 将照片发布到该页面
  • 谢谢你,我会尝试你的建议。
  • 谢谢@deesarus。现在,我可以获得 page_id,但我不知道如何在我的代码中使用它。
  • 我将 mAsyncRunner.request(null, params, "POST", new RequestListener() { ... 更改为 mAsyncRunner.request(pageid + "/feed", params, "POST", new RequestListener() { ... ,但我收到 Facebook 错误:不支持的方法,photos.upload。

标签: android facebook facebook-wall


【解决方案1】:

这是我用于获取应用程序页面 ID 的代码:

private JSONObject getJSONfromUrl(String url) throws IOException {
    InputStream is = new URL(url).openStream();
    try {
      BufferedReader rd = new BufferedReader(new InputStreamReader(is,      Charset.forName("UTF-8")));
      String jsonText = readAll(rd);
      JSONObject json = new JSONObject(jsonText);
      return json;
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
      is.close();
    }
    return null;
}

private String getPageId(String url) {
    String identifier = url.substring(url.lastIndexOf("/"));
    String graphUrl = "https://graph.facebook.com/" + identifier;

    JSONObject response;
    String id = null;
    try {
       response = getJSONfromUrl(graphUrl);
       id = (String) response.get("id");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return id;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多