Android 需要帮助发布到Facebook用户墙吗

Android 需要帮助发布到Facebook用户墙吗,android,facebook,post,Android,Facebook,Post,可能重复: 我使用以下代码发布到Facebook用户墙。我已在HomeActivity中验证了该用户。将accesstoken存储在SharedReferences中 if (HomeActivity.authenticatedFacebook.isSessionValid()) { Log.v("****session valid **** ", "valid"); fb

可能重复:

我使用以下代码发布到Facebook用户墙。我已在HomeActivity中验证了该用户。将accesstoken存储在SharedReferences中

     if (HomeActivity.authenticatedFacebook.isSessionValid()) {
                            Log.v("****session valid **** ", "valid");
                            fbuserpref = getSharedPreferences(FBUSER_PREF, 0);
                            acc_tock = fbuserpref.getString("access_token", " ");
                            Log.v("FB accesstoken  >> In FBShare", acc_tock);


                            if (acc_tock == null && acc_tock == " ") {
                                Log.v("Access token is null in FBShare", "null");
                            } else {
                                AsyncFacebookRunner asyn = new AsyncFacebookRunner(
                                        authenticatedFacebook);                 
                                Bundle parameters = new Bundle();
                                parameters.putString("message", msg.getText()
                                        .toString());
                                        Log.v("target id", HomeActivity.id_json);

                                parameters.putString("target_id", HomeActivity.id_json);

                                parameters.putString("method", "stream.publish");
                                asyn.request("me/feed", parameters, "POST",
                                        new TestRequestListener(), null);


                            }

                        } else {
                            Log.v("session Invalid", "Invalid");
                            Toast.makeText(getApplicationContext(),
                                    "Please Login to Facebook.", Toast.LENGTH_LONG)
                                    .show();
                        }



                            public class TestRequestListener implements RequestListener {

                @Override
                public void onComplete(String response, Object state) {
                    // TODO Auto-generated method stub

                    Log.v("posted successfully", response);
                    finish();
                }

                @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

                }

                @Override
                public void onFacebookError(FacebookError e, Object state) {
                    // TODO Auto-generated method stub

                }       


}
我得到了以下回应

03-03 02:21:05.650: V/posted successfully(26847): {"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}
试试这个-

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

另外,请检查示例。

感谢您的回复。我成功了。只需更改Facebook.java类。将默认的验证活动代码更改为强制对话框验证。它和我的代码配合得很好。不客气…!!!:)