Post To Facebook After Login Fails Android
In my postToFacebook() method. I checked if the user is connected if it is i post a link on my wall. When the user is not signed in, The facebook sdk login progress pop up and i lo
Solution 1:
I have tested this code for posting with Facebook SDK 3.0 and this is working.
Reference the Facebook SDK 3.0 Library project in your current project. Set the AppId in your String.xml file:
<string name="app_id">123456789012345</string>
After that add a MetaTag in your manifest file:
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
For Logging and and Logging out, you can simply use Facebook Login Button widget "com.facebook.widget.LoginButton".
This widget manages the Sessions automatically, so you do-not have to worry about that. Just place this button in the right place in your Layout.xml file. Initialize this button like any other buttons:
LoginButtonauthButton= (LoginButton) findViewById(R.id.YOUR_ID);
After initializing, set permissions on the LoginButton
authButton.setPublishPermissions(Arrays.asList("publish_stream","read_stream"));
and implement setSessionStatusCallback method of the Login button widget:
authButton.setSessionStatusCallback(new Session.StatusCallback()
{
publicvoid call(Session session, SessionState state,Exceptionexception)
{
// TODO Auto-generated method stubif (session.isOpened())
{
Log.i(TAG, "Access Token" + session.getAccessToken());
Request.executeMeRequestAsync(session,new Request.GraphUserCallback() {
publicvoid onCompleted(GraphUser user,Response response)
{
// TODO Auto-generated method stubif (user != null)
{
Log.i(TAG, "User ID " + user.getId());
Log.i(TAG,"Email "+ user.asMap().get("email"));
}
}});
}
else
{
//This else condition will be executed when logout will be clicked.
}
}
});
Below is the method for posting on Facebook.
privatevoidpublishStory(String status)
{
Session session = Session.getActiveSession();
if (session != null)
{
Bundle postParams = newBundle();
postParams.putString("message", status);
Request.Callback callback = newRequest.Callback()
{
publicvoidonMalformedURLException(MalformedURLException e)
{
}
publicvoidonIOException(IOException e)
{
}
publicvoidonFileNotFoundException(FileNotFoundExceptione)
{
}
publicvoidonFacebookError(FacebookError e)
{
}
publicvoidonCompleted(Response response)
{
JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
String postId = null;
try
{
postId = graphResponse.getString("id");
}
catch (JSONException e)
{
Log.i("JSON", "JSON error " + e.getMessage());
}
FacebookRequestError error = response.getError();
Log.e("post response", response.toString());
if (error != null)
{
}
else
{
}
}
};
Request request = newRequest(session, "me/feed", postParams, HttpMethod.POST, callback);
RequestAsyncTask task = newRequestAsyncTask(request);
task.execute();
}
}
Post a Comment for "Post To Facebook After Login Fails Android"