Twitter4j,“查询中指定的术语太多”

时间:2012-05-23 02:34:57

标签: java android twitter4j

我使用以下方法获取关注经过身份验证的用户的所有用户的屏幕名称。

private void getFollowing() {   
     Twitter t = new TwitterFactory().getInstance();
     t.setOAuthConsumer(OAUTH.CONSUMER_KEY, OAUTH.CONSUMER_SECRET);
     aToken = getToken();
     t.setOAuthAccessToken(aToken);
     try {
        long[] friendsID = t.getFriendsIDs(userID, -1).getIDs();
        ResponseList<User> userName = t.lookupUsers(friendsID);
        int count = 0;
        for (User u : userName) {
            count++;
            Log.d("USERNAME : "+ Integer.toString(count), u.getScreenName());
        }
     } catch (TwitterException e) {
        e.printStackTrace();
    }
}

t.lookupUsers(friendsID)会导致以下错误。

W/System.err(16076): {"errors":[{"code":18,"message":"Too many terms specified in query"}]}

据我了解,lookupUsers()方法一次最多可返回100个用户的信息。我提供的不止这些。这可能是为什么?如果是这样,我如何限制原始请求并循环其余用户以获取所有屏幕名称?

如果我错误地知道为什么我会收到错误,那我还有什么不对的?

ANSWER

    private void getFollowing() {   
         Twitter t = new TwitterFactory().getInstance();
         t.setOAuthConsumer(OAUTH.CONSUMER_KEY, OAUTH.CONSUMER_SECRET);
         aToken = getToken();
         t.setOAuthAccessToken(aToken);
         ArrayList<String> names = new ArrayList<String>();
         try {
            int start = 0;
            int finish = 100;
            ArrayList<Long> IDS = new ArrayList<Long>();
            long[] friendsID =  t.getFriendsIDs(userID, -1).getIDs();
            boolean check = true;
            while (check) {
                for (int i=start;i<finish;i++) {
//get first 100     
                    IDS.add(friendsID[i]);
//if at the end, stop
                    if (friendsID.length-1 == i) {
                        check = false;
                        break;                      
                    }
                }
//set values for next 100
                start = start+100;
                finish = finish+100;
                long[] ids = Longs.toArray(IDS);
                ResponseList<User> userName = t.lookupUsers(ids);
//clear so long[] holds max 100 at any given time
                IDS.clear();
                for (User u : userName) {
                    names.add(u.getScreenName());
                }
            }
            String[] screenNames = (String[]) names.toArray(new String[names.size()]);

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, screenNames);
            mPreview.setAdapter(adapter);
         } catch (TwitterException e) {
            e.printStackTrace();
        }
    }

1 个答案:

答案 0 :(得分:2)

private void getFollowing() {   
     Twitter t = new TwitterFactory().getInstance();
     t.setOAuthConsumer(OAUTH.CONSUMER_KEY, OAUTH.CONSUMER_SECRET);
     aToken = getToken();
     t.setOAuthAccessToken(aToken);
     ArrayList<String> names = new ArrayList<String>();
     try {
        int start = 0;
        int finish = 100;
        ArrayList<Long> IDS = new ArrayList<Long>();
        long[] friendsID =  t.getFriendsIDs(userID, -1).getIDs();
        boolean check = true;
        while (check) {
            for (int i=start;i<finish;i++) {
                //get first 100     
                IDS.add(friendsID[i]);
                //if at the end, stop
                if (friendsID.length-1 == i) {
                    check = false;
                    break;                      
                }
            }
            //set values for next 100
            start = start+100;
            finish = finish+100;
            long[] ids = Longs.toArray(IDS);
            ResponseList<User> userName = t.lookupUsers(ids);
            //clear so long[] holds max 100 at any given time
            IDS.clear();
            for (User u : userName) {
                names.add(u.getScreenName());
            }
        }
        String[] screenNames = (String[]) names.toArray(new String[names.size()]);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, screenNames);
        mPreview.setAdapter(adapter);
     } catch (TwitterException e) {
        e.printStackTrace();
    }
}