0

我正在尝试将用户使用我的 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);
}
4

1 回答 1

0

这是我用于获取应用程序页面 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;
}
于 2012-09-20T09:14:31.370 回答