huuanh1987 / facebook-java-api

Automatically exported from code.google.com/p/facebook-java-api
0 stars 0 forks source link

ClassCastException When using JSONClient and a boolean response #239

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Initiate a FacebookJsonRestClient
2. try to call USERS_HAS_APP_PERMISSION on a permission that would return
true (this method returns 1 or 0)
3. the FacebookJsonRestClientBase.parseCallResult parses the "1" string to
Integer as expected.
4. A cast is made to Boolean and a ClassCastException is thrown.

What is the expected output? What do you see instead?
Expected a Boolean object of "true", instead an exception.

What version of the product are you using? On what operating system?
3.0.1-SNAPSHOT, jdk1.6.0_16, Windows Vista x32.

Please provide any additional information below.

The error resides in
ExtensibleClient.protected boolean extractBoolean( String result )

It seems that the FacebookJsonRestClientBase.parseCallResult(String) is
openly parsing primitive results (i.e. integers) to a primitive object
representation, so how does the calling method allow itself to simply cast
to boolean?
This method should work just like
ExtensibleClient's private static boolean extractBoolean( Document result ) 
by using 
return 1 == extractInt(result).

Original issue reported on code.google.com by enricoma...@gmail.com on 7 Sep 2009 at 6:40

GoogleCodeExporter commented 8 years ago
I have the same problem, also with 3.0.1-SNAPSHOT but using Linux.

I don't seem to get the problem when checking the STATUS_UPDATE permission, 
only when
checking the PUBLISH_STREAM permission.

See this thread for details:
http://groups.google.com/group/facebook-java/browse_thread/thread/1638b329af3353
07

Original comment by zyy...@gmail.com on 14 Sep 2009 at 7:08

GoogleCodeExporter commented 8 years ago
Here is a quick fix for the function extractBoolean in the file 
ExtensibleClient.java:

   protected boolean extractBoolean( String result ) throws FacebookException {
     if ( "json".equals( responseFormat ) ) {
-      return (Boolean) FacebookJsonRestClientBase.parseCallResult( result );
+      return (Integer) FacebookJsonRestClientBase.parseCallResult( result )
+        == 1 ? true : false;
     } else {
       FacebookXmlRestClient xmlClient = new FacebookXmlRestClient( this );
       return extractBoolean( xmlClient.parseCallResult( result ) );

Works for me(tm)

Original comment by zyy...@gmail.com on 15 Sep 2009 at 12:17

GoogleCodeExporter commented 8 years ago
You don't need the "? true : false;"
It's silly returning something that evaluates to:
(if (x) return x else return !x)
you can simply return x...

But yeah, that's what I did too a while ago, and it works fine..
The problem is that I can't just add the maven source and have the entire 
company
simply use maven to update.
I had to add the jar manually to our repository, give it a fake version number 
to
make the deployment simple...

Original comment by enricoma...@gmail.com on 15 Sep 2009 at 12:29

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
D'oh!

return ((Integer) FacebookJsonRestClientBase.parseCallResult( result )) == 1;

it is

Original comment by zyy...@gmail.com on 15 Sep 2009 at 12:38

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
The previously proposed solutions are only a temporary fix for this specific 
case.
Making this change causes issues in other areas of the API such as
ExtensibleClient.auth_revokeAuthorization(). This change now causes that method 
to
toss a ClassCastException:

11:39:52,188 ERROR [STDERR] TYPE: class java.lang.ClassCastException
11:39:52,188 ERROR [STDERR] FROM:
com.google.code.facebookapi.ExtensibleClient.extractBoolean(ExtensibleClient.jav
a:2810)
11:39:52,188 ERROR [STDERR] FROM:
com.google.code.facebookapi.ExtensibleClient.auth_revokeAuthorization(Extensible
Client.java:2027)
11:39:52,188 ERROR [STDERR] FROM:
com.google.code.facebookapi.SpecificReturnTypeAdapter.auth_revokeAuthorization(S
pecificReturnTypeAdapter.java:122)
...
11:39:52,189 ERROR [STDERR] MSG: java.lang.Boolean cannot be cast to 
java.lang.Integer

I working fix for both problems really should be addressed in the
ExtensibleClient.users_hasAppPermission(Permission perm, Long userId) method. 
The
Facebook API states that this call returns 1 (true) or 0 (false), therefore this
method should use the extractInt() method instead of extractBoolean().

public boolean users_hasAppPermission( Permission perm, Long userId ) throws
FacebookException {
    if ( userId != null ) {
+       int result = extractInt( callMethod(
FacebookMethod.USERS_HAS_APP_PERMISSION_NOSESSION, newPair( "ext_perm",
perm.getName() ), newPair( "uid", userId ) ) );
+       return (result == 1);
-       return extractBoolean( callMethod(
FacebookMethod.USERS_HAS_APP_PERMISSION_NOSESSION, newPair( "ext_perm",
perm.getName() ), newPair( "uid", userId ) ) );
    } else {
+       int result = extractInt( callMethod( 
FacebookMethod.USERS_HAS_APP_PERMISSION,
newPair( "ext_perm", perm.getName() ) ) );
+       return (result == 1);
-       return extractBoolean( callMethod( 
FacebookMethod.USERS_HAS_APP_PERMISSION,
newPair( "ext_perm", perm.getName() ) ) );
    }
}

Let me know if anyone has issues with this fix.

Original comment by mcdonald...@gmail.com on 13 Oct 2009 at 10:49

GoogleCodeExporter commented 8 years ago
fixed

Original comment by fern...@gmail.com on 31 Oct 2009 at 12:33