我使用以下方法来获取正在通过身份验证的用户的用户的所有屏幕名称。
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个用户的信息。我提供的不只是这些。这可能是为什么吗?如果是这样,我如何限制原始请求并遍历其余用户以获得所有屏幕名称?如果我错了为什么会出错,那我还做错了什么?
回答
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();
}
}
最佳答案
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();
}
}