Android : Error publishing Facebook custom story from Android App

on Sunday, March 22, 2015


i am trying to post a custom story. So far i created a a new story on the open graph section of the developer section. the action is "votar" and the object is "Encuesta", the namespace is "idkl_sharetest".


the following are two functions, the first one is to publish an image on the wall and is working fine.



public void postImage() {
if (checkPermissions()) {
Bitmap img = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
Request uploadRequest = Request.newUploadPhotoRequest(
Session.getActiveSession(), img, new Request.Callback() {
@Override
public void onCompleted(Response response) {
Toast.makeText(MainActivity.this,
"Photo uploaded successfully",
Toast.LENGTH_LONG).show();
}
});
uploadRequest.executeAsync();
} else {
requestPermissions();
}
}


the second one is to publish a custom story (image, title, description) and the app closes.



public void postStory() {
if (checkPermissions()) {

try {

RequestBatch requestBatch = new RequestBatch();

Bundle imageParams = new Bundle();
Bitmap image = BitmapFactory.decodeResource(this.getResources(),
R.drawable.logo);
imageParams.putParcelable("file", image);

Request.Callback imageCallback = new Request.Callback() {

@Override
public void onCompleted(Response response) {
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(MainActivity.this,
"Error1" + error,
Toast.LENGTH_LONG).show();
Log.i(TAG, error.getErrorMessage());
}
}
};

Request imageRequest = new Request(Session.getActiveSession(),
"me/staging_resources", imageParams,
HttpMethod.POST, imageCallback);

imageRequest.setBatchEntryName("imageUpload");

requestBatch.add(imageRequest);

JSONObject encuesta = new JSONObject();
encuesta.put("image", "{result=imageUpload:$.uri}");
encuesta.put("title", "Nueva encuesta");
encuesta.put("type", "encuesta.other");
encuesta.put("description", "Texto para nueva encuesta");

Bundle objectParams = new Bundle();
objectParams.putString("object", encuesta.toString());

Request.Callback objectCallback = new Request.Callback() {

@Override
public void onCompleted(Response response) {
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(MainActivity.this,
"Error2" + error,
Toast.LENGTH_LONG).show();
Log.i(TAG, error.getErrorMessage());
}
}
};

Request objectRequest = new Request(Session.getActiveSession(),
"me/objects/encuesta.other", objectParams,
HttpMethod.POST, objectCallback);

objectRequest.setBatchEntryName("objectCreate");

requestBatch.add(objectRequest);

Bundle actionParams = new Bundle();
actionParams.putString("other", "{result=objectCreate:$.id}");
actionParams.putString("fb:explicitly_shared", "true");

Request.Callback actionCallback = new Request.Callback() {

@Override
public void onCompleted(Response response) {

FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(MainActivity.this,
"Error3" + error,
Toast.LENGTH_LONG).show();
} else {
String actionId = null;
try {
JSONObject graphResponse = response
.getGraphObject()
.getInnerJSONObject();
actionId = graphResponse.getString("id");
} catch (JSONException e) {
Log.i(TAG,
"JSON error "+ e.getMessage());
}
Toast.makeText(getApplicationContext(),
actionId,
Toast.LENGTH_LONG).show();
}
}
};

Request actionRequest = new Request(Session.getActiveSession(),
"me/idkl_sharetest:votar", actionParams, HttpMethod.POST,
actionCallback);

requestBatch.add(actionRequest);

requestBatch.executeAsync();
} catch (JSONException e) {
Log.i(TAG,
"JSON error "+ e.getMessage());
}


} else {
requestPermissions();
}


}


running the app on Android Virtual Device gets the following error code


03-20 18:46:38.995: E/AndroidRuntime(1102): FATAL EXCEPTION: main 03-20 18:46:38.995: E/AndroidRuntime(1102): Process: com.example.sharetest, PID: 1102 03-20 18:46:38.995: E/AndroidRuntime(1102): java.lang.NullPointerException 03-20 18:46:38.995: E/AndroidRuntime(1102): at com.example.sharetest.MainActivity$8.onCompleted(MainActivity.java:219) 03-20 18:46:38.995: E/AndroidRuntime(1102): at com.facebook.Request$4.run(Request.java:1668) 03-20 18:46:38.995: E/AndroidRuntime(1102): at android.os.Handler.handleCallback(Handler.java:733) 03-20 18:46:38.995: E/AndroidRuntime(1102): at android.os.Handler.dispatchMessage(Handler.java:95) 03-20 18:46:38.995: E/AndroidRuntime(1102): at android.os.Looper.loop(Looper.java:136) 03-20 18:46:38.995: E/AndroidRuntime(1102): at android.app.ActivityThread.main(ActivityThread.java:5017) 03-20 18:46:38.995: E/AndroidRuntime(1102): at java.lang.reflect.Method.invokeNative(Native Method) 03-20 18:46:38.995: E/AndroidRuntime(1102): at java.lang.reflect.Method.invoke(Method.java:515) 03-20 18:46:38.995: E/AndroidRuntime(1102): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 03-20 18:46:38.995: E/AndroidRuntime(1102): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 03-20 18:46:38.995: E/AndroidRuntime(1102): at dalvik.system.NativeStart.main(Native Method)


thanks for helping


0 comments:

Post a Comment