Im using the following method to get all the screen names of users who are following the authenticated user.
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) causes the following error.
W/System.err(16076): {"errors":[{"code":18,"message":"Too many terms specified in query"}]}
From what I understand, the lookupUsers() method will return info on up to 100 users at a time. Im providing more than that. Could this be why? If so, how can I limit the original request and loop over the remaining users to get all their screennames?
If I’m wrong about why I’m getting the error, what else am I doing wrong?
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 Answer