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

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

问题描述

我试图整合张贴到自己的墙壁从我的应用程序。我已经有一个区域,用户可以保存他/她的用户名和密码(加密)。我想我的程序调用已保存的用户名和密码,传递给Facebook的身份验证,然后允许应用发布简单的文本(也许链接太)到用户的墙。

这是说,我读过所有的开发者在网页上的Facebook(这个API看起来完全陌生的我...我以前从来没有做过任何类型的网络应用程序开发的...只是桌面应用程序),并尝试与Java库 但说实话,我不明白,任何的各种实现的。有些人声称是简单易用,但显然他们是在我头顶一路。

我甚至试图与Facebook官方的Andr​​oid SDK搞乱,而是采用了web视图界面,​​而我不能传递用户名和密码,方便验证。另外,我还是无能,如何即使正确的认证张贴到墙上。

请帮忙。 谢谢你。

哦,顺便说一句,我已经有一个Facebook的API密钥和应用程序ID。

[更新1]

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

  Facebook的facebookClient =新的Facebook();
facebookClient.authorize(这一点,[APP ID],新的String [] {publish_stream,read_stream,offline_access},这一点);
 

,其中本是一个活动,实现了DialogListener,和[APP ID]是我的Facebook应用程序ID。

感谢。

[更新2]

我找到了解决(见下文),但唯一缺少的就是与我已存储在应用程序中的数据自动填充登录文本框的能力。官方的Facebook的Andr​​oid SDK中可能没有考虑到这一点。我会继续寻找到它。

解决方案

我想通了,与汤姆的帮助(谢谢)。关键是创造与stream.publishAPI调用一个对话框,使用Facebook的Andr​​oid SDK。具体操作步骤如下:

  1. 下载官方Facebook的Andr​​oid SDK中:<一href="http://github.com/facebook/facebook-android-sdk">http://github.com/facebook/facebook-android-sdk
  2. 导入项目文件到Eclipse中。
  3. 该项目导出为* .jar文件。 (这可能会导致冲突)

    [更新]

    Facebook最近更新了源$ C ​​$ C,我注意到图标文件造成的资源ID的冲突与我的项目(​​Android的1.5 +)。我的解决办法就是忘记导出为一个罐子。相反,直接复制Facebook的COM文件夹到你的应用程序的SRC文件夹(即com.facebook.android应该是一个包在你的应用程序...对旁边的源文件)。如果你已经在你的SRC文件夹中的COM文件夹,不用担心出现约覆盖文件任何对话框,不关你的源文件应该被覆盖。返回到Eclipse中,并刷新SRC文件夹和com.facebook.android现在应该列为包。复制的包括Facebook的图标,你的应用程序的绘制文件夹之一,并刷新这一点。 Eclipse会抱怨的FbDialog.java文件...只是导入指向添加到您的应用程序的R文件,该文件的标题(例如,如果您的应用程序包的名称是com.android.myapp,然后添加这样的:进口com.android.myapp.R;)。转至#5,如果你需要做到这一点。

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

  5. 请看下面的简单示例code:


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

公共类FacebookActivity扩展活动实现DialogListener,
        OnClickListener
{

    私人facebook facebookClient;
    私人的LinearLayout facebookButton;

    @覆盖
    保护无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.test); //我的布局XML

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

    }

    @覆盖
    公共无效的onComplete(束值)
    {

        如果(values​​.isEmpty())
        {
            //跳过点击?
            返回;
        }

        //如果facebookClient.authorize(...)是成功的,这种运行
        //成功后在此之后也跑
        //张贴后,的post_id被加到值束
        //我使用从电话之间的区别
        // faceBook.authorize(...),并从成功后打电话
        //是否有这样做的更好的办法?
        如果(!values​​.containsKey(的post_id))
        {
            尝试
            {
                捆绑参数=新包();
                parameters.putString(消息,这是一个测试); //张贴到墙上的消息
                facebookClient.dialog(这一点,stream.publish,参数,这一点); //stream.publish是一个API调用
            }
            赶上(例外五)
            {
                // TODO:处理异常
                的System.out.println(e.getMessage());
            }
        }
    }

    @覆盖
    公共无效onerror的(DialogError E)
    {
        的System.out.println(错误:+ e.getMessage());
    }

    @覆盖
    公共无效onFacebookError(FacebookError E)
    {
        的System.out.println(错误:+ e.getMessage());
    }

    @覆盖
    公共无效OnCancel的()
    {
    }

    @覆盖
    公共无效的onClick(视图v)
    {
        如果(V == facebookButton)
        {
            facebookClient =新的Facebook();
            //替换为您自己APP_API_ID
            facebookClient.authorize(这一点,APP_API_ID,
                新的String [] {publish_stream,read_stream,offline_access},这一点);
        }
    }
}

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.

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.

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.

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

[UPDATE 1]

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);

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

Thanks.

[UPDATE 2]

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.

解决方案

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. 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)

    [UPDATE]

    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.

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

  5. Look at the following simplified example code:


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天全站免登陆