gpt4 book ai didi

java - Twitter4j, "Too many terms specified in query"

转载 作者:行者123 更新时间:2023-11-29 22:06:24 25 4
gpt4 key购买 nike

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

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();
}
}

关于java - Twitter4j, "Too many terms specified in query",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10712742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com