FileDocCategorySizeDatePackage
FacebookJsonRestClient.javaAPI DocGoogle Facebook API v1.512362Sun Nov 11 16:08:58 GMT 2007com.facebook.api

FacebookJsonRestClient

public class FacebookJsonRestClient extends ExtensibleClient
A FacebookRestClient that uses the JSON result format. This means results from calls to the Facebook API are returned as JSON and transformed into Java Object's.

Fields Summary
Constructors Summary
public FacebookJsonRestClient(String apiKey, String secret)
Constructor.

param
apiKey your Facebook API key
param
secret your 'secret' Facebook key

    this(SERVER_URL, apiKey, secret, null);
  
public FacebookJsonRestClient(String apiKey, String secret, String sessionKey)
Constructor.

param
apiKey your Facebook API key
param
secret your 'secret' Facebook key
param
sessionKey the session-id to use

    this(SERVER_URL, apiKey, secret, sessionKey);
  
public FacebookJsonRestClient(String serverAddr, String apiKey, String secret, String sessionKey)
Constructor.

param
serverAddr the URL of the Facebook API server to use
param
apiKey your Facebook API key
param
secret your 'secret' Facebook key
param
sessionKey the session-id to use
throws
MalformedURLException if you specify an invalid URL

    this(new URL(serverAddr), apiKey, secret, sessionKey);
  
public FacebookJsonRestClient(URL serverUrl, String apiKey, String secret, String sessionKey)
Constructor.

param
serverUrl the URL of the Facebook API server to use
param
apiKey your Facebook API key
param
secret your 'secret' Facebook key
param
sessionKey the session-id to use

    super(serverUrl, apiKey, secret, sessionKey);
  
Methods Summary
public java.lang.Stringauth_getSession(java.lang.String authToken)
Sets the session information (sessionKey) using the token from auth_createToken.

param
authToken the token returned by auth_createToken or passed back to your callback_url.
return
the session key
throws
FacebookException
throws
IOException

    if (null != this._sessionKey) {
      return this._sessionKey;
    }
    JSONObject d = (JSONObject)
      this.callMethod(FacebookMethod.AUTH_GET_SESSION, 
                      new Pair<String, CharSequence>("auth_token", authToken.toString()));
    try {
        this._sessionKey = (String) d.get("session_key");
        Object uid = d.get("uid");
        try {
          this._userId = (Integer) uid;
        } catch (ClassCastException cce) {
          this._userId = Integer.parseInt((String) uid);
        } 
        if (this.isDesktop()) {
          this._sessionSecret = (String) d.get("secret");
        }
    }
    catch (Exception ignored) {}
    return this._sessionKey;
  
public java.lang.Stringdata_getUserPreference(java.lang.Integer prefId)

        throw new FacebookException(ErrorCode.GEN_UNKNOWN_METHOD, "The FacebookJsonRestClient does not support this API call.  Please use an instance of FacebookRestClient instead.");
    
public java.util.Mapdata_getUserPreferences()

        throw new FacebookException(ErrorCode.GEN_UNKNOWN_METHOD, "The FacebookJsonRestClient does not support this API call.  Please use an instance of FacebookRestClient instead.");
    
public voiddata_setUserPreference(java.lang.Integer prefId, java.lang.String value)

        throw new FacebookException(ErrorCode.GEN_UNKNOWN_METHOD, "The FacebookJsonRestClient does not support this API call.  Please use an instance of FacebookRestClient instead.");
        
    
public voiddata_setUserPreferences(java.util.Map values, boolean replace)

        throw new FacebookException(ErrorCode.GEN_UNKNOWN_METHOD, "The FacebookJsonRestClient does not support this API call.  " +
                "Please use an instance of FacebookRestClient instead.");
        
    
protected booleanextractBoolean(java.lang.Object val)
Extracts a Boolean from a result that consists of a Boolean only.

param
val
return
the Boolean

    try {
        if (val instanceof String) {
            return ! ((val == null) || (val.equals("false")) || (val.equals("0")));
        }
        return ((Long)val != 0l);
    } catch (ClassCastException cce) {
      logException(cce);
      return false;
    }
  
protected intextractInt(java.lang.Object val)
Extracts an Integer from a result that consists of an Integer only.

param
val
return
the Integer

    try {
      if (val instanceof String) {
          //shouldn't happen, really
          return Integer.parseInt((String)val);
      }
      if (val instanceof Long) {
          //this one will happen, the parse method parses all numbers as longs
          return ((Long)val).intValue();
      }
      return (Integer) val;
    } catch (ClassCastException cce) {
      logException(cce);
      return 0;
    }
  
protected java.lang.LongextractLong(java.lang.Object val)
Extracts a Long from a result that consists of an Long only.

param
val
return
the Integer

    try {
      if (val instanceof String) {
          //shouldn't happen, really
          return Long.parseLong((String)val);
      }
      return (Long) val;
    } catch (ClassCastException cce) {
      logException(cce);
      return null;
    }
  
public java.lang.StringextractString(java.lang.Object val)
Extracts a String from a result consisting entirely of a String.

param
val
return
the String

    try {
      return (String) val;
    } catch (ClassCastException cce) {
      logException(cce);
      return null;
    }
  
protected java.net.URLextractURL(java.lang.Object url)
Extracts a URL from a result that consists of a URL only. For JSON, that result is simply a String.

param
url
return
the URL

    if (!(url instanceof String)) {
      return null;
    }
    return (null == url || "".equals(url)) ? null : new URL( (String) url);
  
public java.lang.StringgetResponseFormat()
The response format in which results to FacebookMethod calls are returned

return
the format: either XML, JSON, or null (API default)

    return "json";
  
public java.util.Listmarketplace_getListings(java.util.List listingIds, java.util.List uids)

        throw new FacebookException(ErrorCode.GEN_UNKNOWN_METHOD, "The FacebookJsonRestClient does not support this API call.  " +
        "Please use an instance of FacebookRestClient instead.");
    
public java.util.Listmarketplace_getSubCategories()

        throw new FacebookException(ErrorCode.GEN_UNKNOWN_METHOD, "The FacebookJsonRestClient does not support this API call.  " +
        "Please use an instance of FacebookRestClient instead.");
    
public java.util.Listmarketplace_search(com.facebook.api.MarketListingCategory category, com.facebook.api.MarketListingSubcategory subcategory, java.lang.String searchTerm)

        throw new FacebookException(ErrorCode.GEN_UNKNOWN_METHOD, "The FacebookJsonRestClient does not support this API call.  " +
        "Please use an instance of FacebookRestClient instead.");
    
protected java.lang.ObjectparseCallResult(java.io.InputStream data, com.facebook.api.IFacebookMethod method)
Parses the result of an API call from JSON into Java Objects.

param
data an InputStream with the results of a request to the Facebook servers
param
method the method
return
a Java Object
throws
FacebookException if data represents an error
throws
IOException if data is not readable
see
JSONObject

    BufferedReader in = new BufferedReader(new InputStreamReader(data, "UTF-8"));
    StringBuffer buffer = new StringBuffer();
    String line;
    while ((line = in.readLine()) != null) {
      buffer.append(line);
    }

    String jsonResp = new String(buffer);
    
    Object json = null;
    if (this.rawResponse.matches("[\\{\\[].*[\\}\\]]")) {
        try {
            if (this.rawResponse.matches("\\{.*\\}")) {
                json = new JSONObject(jsonResp);
            }
            else {
                json = new JSONArray(jsonResp);
            }
        }
        catch (Exception ignored) { ignored.printStackTrace(); }
    }
    else {
        if (this.rawResponse.startsWith("\"")) {
            this.rawResponse = this.rawResponse.substring(1);
        }
        if (this.rawResponse.endsWith("\"")) {
            this.rawResponse = this.rawResponse.substring(0, this.rawResponse.length() - 1);
        }
        try {
            //it's either a number...
            json = Long.parseLong(this.rawResponse);
        }
        catch (Exception e) {
            //...or a string
            json = this.rawResponse;
        }
    }
    if (isDebug()) {
      log(method.methodName() + ": " + (null != json ? json.toString() : "null"));
    }

    if (json instanceof JSONObject) {
      JSONObject jsonObj = (JSONObject) json;
      try {
            Integer errorCode = (Integer) jsonObj.get("error_code");
            if (errorCode != null) {
                String message = (String) jsonObj.get("error_msg");
                throw new FacebookException(errorCode, message);
            }
      }
      catch (JSONException ignored) {
          //the call completed normally
      }
    }
    return json;
  
public java.lang.Integersms_send(java.lang.String message, java.lang.Integer smsSessionId, boolean makeNewSession)

        throw new FacebookException(ErrorCode.GEN_UNKNOWN_METHOD, "The FacebookJsonRestClient does not support this API call.  " +
        "Please use an instance of FacebookRestClient instead.");
    
public java.lang.Integersms_send(java.lang.Long userId, java.lang.String message, java.lang.Integer smsSessionId, boolean makeNewSession)

        throw new FacebookException(ErrorCode.GEN_UNKNOWN_METHOD, "The FacebookJsonRestClient does not support this API call.  " +
        "Please use an instance of FacebookRestClient instead.");