2013-07-29 37 views
0

我開始使用Twitter4J,我想弄清楚每種方法的作用。 我執行裏面有很多的沒有什麼JavaDoc的方法UserStreamListener ,,如:Twitter4J,關於可能的字段的文檔

public void onException(Exception ex) { 
public void onBlock(User arg0, User arg1) 
public void onDeletionNotice(long arg0, long arg1) 
public void onDirectMessage(DirectMessage arg0) 
... 

所以,當他們確切地執行,什麼意思PARAMS我不知道。我下載了Twitter4J的代碼,並且我看到了一個可以看到圖書館如何生成事件的類,但是當我嘗試將這些信息與Twitter關聯時,我沒有找到更多信息。

我發現此網頁https://dev.twitter.com/docs/platform-objects/tweets,但它不是這個信息。

public final class JSONObjectType { 
    public enum Type { 
     SENDER, 
     STATUS, 
     DIRECT_MESSAGE, 
     DELETE, 
     LIMIT, 
     STALL_WARNING, 
     SCRUB_GEO, 
     FRIENDS, 
     FAVORITE, 
     UNFAVORITE, 
     FOLLOW, 
     UNFOLLOW, 
     USER_LIST_MEMBER_ADDED, 
     USER_LIST_MEMBER_DELETED, 
     USER_LIST_SUBSCRIBED, 
     USER_LIST_UNSUBSCRIBED, 
     USER_LIST_CREATED, 
     USER_LIST_UPDATED, 
     USER_LIST_DESTROYED, 
     USER_UPDATE, 
     BLOCK, 
     UNBLOCK, 
     DISCONNECTION, 
     UNKNOWN 
    } 


    private static final Logger logger = Logger.getLogger(JSONObjectType.class); 

    /** 
    * Determine the respective object type for a given JSONObject. This 
    * method inspects the object to figure out what type of object it 
    * represents. This is useful when processing JSON events of mixed type 
    * from a stream, in which case you may need to know what type of object 
    * to construct, or how to handle the event properly. 
    * 
    * @param json the JSONObject whose type should be determined 
    * @return the determined JSONObjectType, or null if not recognized 
    */ 
    public static Type determine(JSONObject json) { 
     // This code originally lived in AbstractStreamImplementation. 
     // I've moved it in here to expose it as a public encapsulation of 
     // the object type determination logic. 
     if (!json.isNull("sender")) { 
      return Type.SENDER; 
     } else if (!json.isNull("text")) { 
      return Type.STATUS; 
     } else if (!json.isNull("direct_message")) { 
      return Type.DIRECT_MESSAGE; 
     } else if (!json.isNull("delete")) { 
      return Type.DELETE; 
     } else if (!json.isNull("limit")) { 
      return Type.LIMIT; 
     } else if (!json.isNull("warning")) { 
      return Type.STALL_WARNING; 
     } else if (!json.isNull("scrub_geo")) { 
      return Type.SCRUB_GEO; 
     } else if (!json.isNull("friends")) { 
      return Type.FRIENDS; 
     } else if (!json.isNull("event")) { 
      String event; 
      try { 
       event = json.getString("event"); 
       if ("favorite".equals(event)) { 
        return Type.FAVORITE; 
       } else if ("unfavorite".equals(event)) { 
        return Type.UNFAVORITE; 
       } else if ("follow".equals(event)) { 
        return Type.FOLLOW; 
       } else if ("unfollow".equals(event)) { 
        return Type.UNFOLLOW; 
       } else if (event.startsWith("list")) { 
        if ("list_member_added".equals(event)) { 
         return Type.USER_LIST_MEMBER_ADDED; 
        } else if ("list_member_removed".equals(event)) { 
         return Type.USER_LIST_MEMBER_DELETED; 
        } else if ("list_user_subscribed".equals(event)) { 
         return Type.USER_LIST_SUBSCRIBED; 
        } else if ("list_user_unsubscribed".equals(event)) { 
         return Type.USER_LIST_UNSUBSCRIBED; 
        } else if ("list_created".equals(event)) { 
         return Type.USER_LIST_CREATED; 
        } else if ("list_updated".equals(event)) { 
         return Type.USER_LIST_UPDATED; 
        } else if ("list_destroyed".equals(event)) { 
         return Type.USER_LIST_DESTROYED; 
        } 
       } else if ("user_update".equals(event)) { 
        return Type.USER_UPDATE; 
       } else if ("block".equals(event)) { 
        return Type.BLOCK; 
       } else if ("unblock".equals(event)) { 
        return Type.UNBLOCK; 
       } 
      } catch (JSONException jsone) { 
       try { 
        logger.warn("Failed to get event element: ", json.toString(2)); 
       } catch (JSONException ignore) { 
       } 
      } 
     } else if (!json.isNull("disconnect")) { 
      return Type.DISCONNECTION; 
     } 
     return Type.UNKNOWN; 
    } 
} 

例如,如果JSON來了「event」和「block」,方法onBlock將會執行。但是,有關Tweet中所有可能字段的信息在哪裏?

回答

0

您可能會發現Twitter的Streaming message types文檔中需要的信息,特別是,看看在Events部分描述了消息格式:

{ 
    "target": TARGET_USER, 
    "source": SOURCE_USER, 
    "event":"EVENT_NAME", 
    "target_object": TARGET_OBJECT, 
    "created_at": "Sat Sep 4 16:10:54 +0000 2010" 
} 

此外,該文檔UserStreamListener將有助於參數名的方法,例如onBlock方法:

onBlock(User source, User blockedUser)