分享从Facebook墙上的webservice获得的数据

时间:2013-01-30 05:19:41

标签: android facebook json

我已经解析了从webservice获得的数据,当我点击详细活动页面中的任何列表项目时,它在listview中显示,我需要在Facebook上分享该项目的内容。我尝试使用以下代码,但我无法在Facebook墙上共享数据。任何人可以帮我这个吗?答案将适用

详细活动

public class Singlemenuitem  extends Activity {
// Your Facebook APP ID
    private static String APP_ID = "308180782571605"; // Replace with your App ID

    // Instance of Facebook Class
    private Facebook facebook = new Facebook(APP_ID);
    private AsyncFacebookRunner mAsyncRunner;
    String FILENAME = "AndroidSSO_data";
    private SharedPreferences mPrefs;


static  String TAG_URL  = "url";






    public String description,title;

// JSON node keys
static final String TAG_TITLE = "title";
static final String TAG_DATE = "date";
static final String TAG_NAME = "name";
static final String TAG_CONTENT = "content";

LinearLayout line1,line2,line3;
   Button btnShare;
   Button btnInvite;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_list_item);


 // getting intent data
    Intent in = getIntent();
    final  String url1 = in.getStringExtra(TAG_URL);

    ImageView imgv = (ImageView) findViewById(R.id.imgdesc);
    ImageLoader imageLoader = new  ImageLoader(getApplicationContext());
    imageLoader.DisplayImage(url1, imgv);





   // Get JSON values from previous intent
    final String title = in.getStringExtra(TAG_TITLE);
    String date = in.getStringExtra(TAG_DATE);
    String name = in.getStringExtra(TAG_NAME);
    final String content = in.getStringExtra(TAG_CONTENT);
    System.out.println("bool " +title +" "+date+" "+name +" " +content);
    // Displaying all values on the screen
    TextView lblName = (TextView) findViewById(R.id.name_label);
    TextView lblCost = (TextView) findViewById(R.id.email_label);
    TextView lblDesc = (TextView) findViewById(R.id.mobile_label);
    TextView lblCont = (TextView) findViewById(R.id.content_label);

    lblName.setText(title);
    lblCost.setText(date);
    lblDesc.setText(name);
    lblCont.setText(content);




        //share

    ImageView facebook11=(ImageView) findViewById(R.id.facebook);
    facebook11.setOnClickListener(new View.OnClickListener() {

        //@Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Log.d("Image Button", "button Clicked");
            loginToFacebook();

            if (facebook.isSessionValid()) {
                postToWall();




              }
        }       

    });


}           







public void loginToFacebook() {
    mPrefs = getPreferences(MODE_PRIVATE);
    String access_token = mPrefs.getString("access_token", null);
    long expires = mPrefs.getLong("access_expires", 0);

    if (access_token != null) {
        facebook.setAccessToken(access_token);
    }

    if (expires != 0) {
        facebook.setAccessExpires(expires);
    }

    if (!facebook.isSessionValid()) {
        facebook.authorize(this, new String[] { "feed" }, new DialogListener() {

            public void onCancel() {
                // Function to handle cancel event
            }

            public void onComplete(Bundle values) {
                // Function to handle complete event
                // Edit Preferences and update facebook acess_token
                SharedPreferences.Editor editor = mPrefs.edit();
                editor.putString("access_token", facebook.getAccessToken());
                editor.putLong("access_expires", facebook.getAccessExpires());
                editor.commit();

                postToWall();
            }


            public void onError(DialogError error) {
                // Function to handle error

            }


            public void onFacebookError(FacebookError fberror) {
                // Function to handle Facebook errors

            }

        });
    }
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    facebook.authorizeCallback(requestCode, resultCode, data);
}
private void postToWall() {
    Bundle parameters = new Bundle();

             parameters.putString("name", "Facebook SDK for Android"); 
    //parameters.putString("title",  lblName.getText().toString());
    //  parameters.putString("title", TAG_TITLE);  
      facebook.dialog(Singlemenuitem.this, "title", parameters, new    DialogListener() {


        public void onFacebookError(FacebookError e) {
            // TODO Auto-generated method stub

        }


        public void onError(DialogError e) {
            // TODO Auto-generated method stub

        }


        public void onComplete(Bundle values) {
            // TODO Auto-generated method stub
        }


        public void onCancel() {
            // TODO Auto-generated method stub
        }
    });
}}

1 个答案:

答案 0 :(得分:4)

我用Facebook Sdk 3.0完成了这件事
提取该jar文件,然后您将找到针对不同Facebook功能的示例代码,您只需要输入 facebook App id

我在登录时遇到错误,所以我做了一些更改,就是保存Facebook的标记:

@Override
protected void onSaveInstanceState(Bundle outState) 
{
    super.onSaveInstanceState(outState);
    Session session = Session.getActiveSession();
    Session.saveSession(session, outState); // Saving Session
}

在您的活动中更改此内容并让我了解此信息。