我正在 Twitter 上开发一个应用程序。
Twitter 在随机时间给我 400 的回复。我很恐慌。他们的服务器太糟糕了,无法满足请求。我正在点击他们的 REST URL 以获取带有光标的关注者。有时我会获得前 100 个关注者,而在第二次游标迭代中我获得了 HTTP 400。
只有一次尝试给了我 300 名追随者。否则一切都以 HTTP 代码 400 结束。我正在使用递归。根据 next_cursor 的值,直到它达到 0。我继续用它的值点击 URL 以获得下一页的关注者。
你们有什么建议?如何克服这种情况。推特服务器回复不好的地方。他们错误地邀请开发人员在他们身上开发应用程序。
是不是我做错了什么。或者它完全是推特问题。
import java.io.ByteArrayInputStream;
导入 java.io.DataInputStream;导入 java.net.URL;导入 java.net.URLConnection;导入 java.util.ArrayList;导入 java.util.List;
导入 org.jdom.Document;导入 org.jdom.Element;导入 org.jdom.input.SAXBuilder;
公共类 Twt {
String urlFlwrs= "http://twitter.com/statuses/followers/tahirakram.xml?cursor=";
List followers = new ArrayList();
long cursorCounter = -1;
public static void main(String[] args) {
long start = System.currentTimeMillis();
new Twt().readFollowFriends();
System.out.printf("Total Time: %d secs", (System.currentTimeMillis() - start)/1000);
}
void readFollowFriends(){
try {
StringBuffer followersData = new StringBuffer();
/* use urlFrnds as a parameter if you want to fetch friends */
URL url = new URL(urlFlwrs+cursorCounter);
URLConnection urlConnection = url.openConnection();
DataInputStream dis = new DataInputStream(urlConnection.getInputStream());
String inputLine;
while ((inputLine = dis.readLine()) != null) {
followersData.append(inputLine);
}
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(new ByteArrayInputStream(followersData.toString().getBytes()));
Element root = document.getRootElement();
Element usersElm = root.getChild("users");
Element nextCursor = root.getChild("next_cursor");
List users = usersElm.getChildren("user");
for (int c = 0; c < users.size(); c++) {
Element user = (Element) users.get(c);
Element name = user.getChild("name");
System.out.println(name.getText());
}
if (nextCursor != null){
cursorCounter = Long.parseLong(nextCursor.getText());
if (cursorCounter != 0)
readFollowFriends();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
java.io.IOException: Server returned HTTP response code: 400 for URL:http://twitter.com/statuses/followers/tahirakram.xml?cursor=1312316779756149406
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1133)
at Twt.readFollowFriends(Twt.java:32)
at Twt.readFollowFriends(Twt.java:62)
at Twt.readFollowFriends(Twt.java:62)
at Twt.main(Twt.java:15)