Android / Java - 发布简单的文字到Facebook墙? [英] Android/Java -- Post simple text to Facebook wall?

查看:104
本文介绍了Android / Java - 发布简单的文字到Facebook墙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将发布内容从我的应用程序中整合到自己的墙上。我已经有一个用户可以保存他/她的用户名和密码(加密)的区域。我希望我的程序调用保存的用户名和密码,将其传递给Facebook进行身份验证,然后允许应用程序向用户的墙发布简单文本(也可能是链接)。

I'm trying to integrate posting to one's wall from within my app. I already have an area where the user can save his/her username and password (encrypted). I would like my program to recall the saved username and password, pass that to Facebook for authentication, and then allow the app to post simple text (maybe a link too) to the user's wall.

那就是说,我已经在Facebook的开发者页面上看过所有内容(api看起来与我完全陌生了...我从来没有做任何类型的网络应用程序开发之前就是桌面应用程序),并尝试使用Java库这里,但是说实话,我不明白任何各种实现。有人声称使用起来很简单,但显然他们都高于我的头。

That said, I've read everything on the developer pages at Facebook (the api looks completely foreign to me... I've never done any type of web app development before... just desktop apps), and experimented with the Java libraries here but to be honest, I don't understand any of the various implementations. Some claim to be simple to use, but apparently they are all way above my head.

我甚至试图搞砸官方的Facebook Android SDK,但是它使用了一个webview界面,我无法传入用户名和密码来进行身份验证。另外,即使在正确的身份验证之后,如何发布到墙上仍然是无能为力的。

I've even tried messing with the official Facebook Android SDK, but that uses a webview interface, and I can't pass in the username and password for easy authentication. Plus, I'm still clueless as to how to post to the wall even after correct authentication.

请帮忙。
谢谢。

Please help. Thanks.

哦,btw我已经有一个Facebook API密钥和应用程序ID。

Oh, btw I already have a Facebook API key and Application ID.

[更新1]

进一步说明:
如果我使用官方Facebook Android SDK http://github.com/facebook/facebook-android-sdk 下一步该怎么做(用户登录后) ?这不清楚我。

For further clarification: If I use the following code snippet with the official Facebook Android SDK http://github.com/facebook/facebook-android-sdk what should I do next (after the user has logged-in)? This is unclear to me.

Facebook facebookClient = new Facebook();
facebookClient.authorize(this, "[APP ID]", new String[] {"publish_stream", "read_stream", "offline_access"}, this);

其中this是实现DialogListener的Activity,[APP ID]是我的Facebook应用程序ID。

where "this" is an Activity that implements a DialogListener, and "[APP ID]" is my Facebook application ID.

谢谢。

[更新2]

我找到了一个解决方案(见下文),尽管唯一缺少的是可以使用我存储在应用程序中的数据自动填充登录文本框。官方Facebook Android SDK可能不允许这样做。我会继续研究它。

I found a solution (see below), though the only thing missing is the ability to auto-populate the login text boxes with the data I have stored in the app. The official Facebook Android SDK may not allow for this. I'll keep looking into it.

推荐答案

我想出了,汤姆的帮助(谢谢)。关键是使用Facebook Android SDK创建一个带有stream.publishAPI调用的对话框。以下是步骤:

I figured it out, with Tom's help (thanks). The key was creating a dialog with the "stream.publish" API call, using the Facebook Android SDK. Here are the steps:


  1. 下载官方Facebook Android SDK: http://github.com/facebook/facebook-android-sdk

  2. 将项目文件导入Eclipse。 li>
  3. 将项目导出为* .jar文件。 (这可能会导致冲突)

  1. Download the official Facebook Android SDK : http://github.com/facebook/facebook-android-sdk
  2. Import the project files into Eclipse.
  3. Export the project as a *.jar file. (this might cause a conflict)

[更新]

Facebook最近更新源代码,我注意到图标文件导致资源ID与我的项目(Android 1.5+)冲突。我的解决方案是忘记导出为jar。相反,将Facebookcom文件夹直接复制到应用程序的src文件夹中(即com.facebook.android应该是您的应用程序中的一个包...与源文件一起)。如果您的src文件夹中已经有一个com文件夹,不用担心任何出现关于覆盖文件的对话框,不应覆盖任何源文件。回到Eclipse,刷新src文件夹,com.facebook.android现在应该被列为一个包。将其中一个附加的Facebook图标复制到应用程序的可绘制文件夹中,并将其刷新。 Eclipse将抱怨FbDialog.java文件...只需将指向您应用程序的R文件的导入添加到该文件的标题(例如,如果您的应用程序的软件包名称为com.android.myapp),则添加这个:import com.android.myapp.R;)。如果需要,请转到#5。

Facebook recently updated the source code and I noticed the icon file caused resource id conflicts with my projects (Android 1.5+). My solution is to forget about exporting as a jar. Instead, copy the Facebook "com" folder directly into your app's "src" folder (i.e. "com.facebook.android" should be a package in your app... right alongside your source files). If you already have a "com" folder in your "src" folder, don't worry about any dialog boxes that appear about overwriting files, none of your source files should be overwritten. Go back into Eclipse, and refresh the "src" folder and "com.facebook.android" should now be listed as a package. Copy one of the included Facebook icons to your app's "drawable" folder and refresh that as well. Eclipse will complain about the "FbDialog.java" file... just add an import pointing to your app's "R" file to the header of that file (e.g. if your app's package name is "com.android.myapp," then add this: "import com.android.myapp.R;"). Go to #5 if you needed to do this.

将.jar文件添加到项目的构建路径

Add the .jar file to your project's build path





import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import com.facebook.android.*;
import com.facebook.android.Facebook.DialogListener;

public class FacebookActivity extends Activity implements DialogListener,
        OnClickListener
{

    private Facebook facebookClient;
    private LinearLayout facebookButton;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.test);//my layout xml

        facebookButton = (LinearLayout)this.findViewById(R.id.Test_Facebook_Layout);

    }

    @Override
    public void onComplete(Bundle values)
    {

        if (values.isEmpty())
        {
            //"skip" clicked ?
            return;
        }

        // if facebookClient.authorize(...) was successful, this runs
        // this also runs after successful post
        // after posting, "post_id" is added to the values bundle
        // I use that to differentiate between a call from
        // faceBook.authorize(...) and a call from a successful post
        // is there a better way of doing this?
        if (!values.containsKey("post_id"))
        {
            try
            {
                Bundle parameters = new Bundle();
                parameters.putString("message", "this is a test");// the message to post to the wall
                facebookClient.dialog(this, "stream.publish", parameters, this);// "stream.publish" is an API call
            }
            catch (Exception e)
            {
                // TODO: handle exception
                System.out.println(e.getMessage());
            }
        }
    }

    @Override
    public void onError(DialogError e)
    {
        System.out.println("Error: " + e.getMessage());
    }

    @Override
    public void onFacebookError(FacebookError e)
    {
        System.out.println("Error: " + e.getMessage());
    }

    @Override
    public void onCancel()
    {
    }

    @Override
    public void onClick(View v)
    {
        if (v == facebookButton)
        {
            facebookClient = new Facebook();
            // replace APP_API_ID with your own
            facebookClient.authorize(this, APP_API_ID,
                new String[] {"publish_stream", "read_stream", "offline_access"}, this);
        }
    }
}

这篇关于Android / Java - 发布简单的文字到Facebook墙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆