Reputation: 16521
Has anyone got an example of how to use the Android Graph API?
I am stuck with the basics, like posting text to the wall on Facebook.
I'm using the Facebook SDK for Android. Here is what I have so far:
Bundle params = new Bundle();
params.putString("message", "picture caption");
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request("me/feed", params, "POST", new RequestListener() {
@Override
public void onMalformedURLException(MalformedURLException e, Object state) {
// TODO Auto-generated method stub
}
@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 onFacebookError(FacebookError e, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onComplete(String response, Object state) {
// TODO Auto-generated method stub
}
}, "foo");
Nothing happens, and my Logcat says:
03-02 22:10:02.973: WARN/Bundle(1930): Key message expected byte[] but value was a java.lang.String. The default value <null> was returned.
03-02 22:10:02.983: WARN/Bundle(1930): Attempt to cast generated internal exception:
03-02 22:10:02.983: WARN/Bundle(1930): java.lang.ClassCastException: java.lang.String
03-02 22:10:02.983: WARN/Bundle(1930): at android.os.Bundle.getByteArray(Bundle.java:1305)
03-02 22:10:02.983: WARN/Bundle(1930): at com.facebook.android.Util.encodePostBody(Util.java:63)
03-02 22:10:02.983: WARN/Bundle(1930): at com.facebook.android.Util.openUrl(Util.java:182)
03-02 22:10:02.983: WARN/Bundle(1930): at com.facebook.android.Facebook.request(Facebook.java:559)
03-02 22:10:02.983: WARN/Bundle(1930): at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:253)
Upvotes: 4
Views: 12577
Reputation: 159
You can do this like :-- give description in bundle message and if u want to share some image from server u can provide link as i given below
Bundle params = new Bundle();
params.putString("message", "description");
params.putString("link","your image url" );
new GraphRequest( AccessToken.getCurrentAccessToken(),
"me/feed",
params,
HttpMethod.POST,
new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
Log.d(TAG, "" + response.toString());
try {
JSONObject jsonObject = new JSONObject(response.getRawResponse());
if (jsonObject != null) {
String postId = jsonObject.getString("id");
if (postId != null && !postId.equalsIgnoreCase("")) {
hideProgressDialog();
Log.d("postId", "" + postId);
} else {
hideProgressDialog();
Utils.showToast(getActivity(), getResources().getString(R.string.txt_try_again));
}
} else {
hideProgressDialog();
Utils.showToast(getActivity(), getResources().getString(R.string.txt_try_again));
}
} catch (JSONException e) {
hideProgressDialog();
e.printStackTrace();
} catch (Throwable e) {
hideProgressDialog();
e.printStackTrace();
}
}
}).executeAsync();
Upvotes: 2
Reputation: 947
try this
Bundle params = new Bundle();
params.putString(Facebook.TOKEN, token);
params.putString("message", "graph api");
mAsyncRunner.request("/me/feed", params,"POST", new SampleUploadListener(),null);
you can get access token by using
token = mFacebook.getAccessToken();
Upvotes: 2