我正在Twitter上开发一个应用程序。
Twitter正在给我随机时间400的响应。我很恐慌。他们的服务器太糟糕了,无法接受请求。我正在使用他们的REST URL来获取光标的关注者。有时我得到前100名粉丝,在第二次光标迭代时,我得到了HTTP 400.
只有一次尝试给了我300个粉丝。否则所有都在HTTP代码400中结束。我正在使用递归。根据next_cursor的值,直到它达到0.我继续用它的值命中URL以获得下一页粉丝。
你们建议的是什么?如何克服这种情况。 Twitter服务器回复不好的地方。他们错误地邀请开发人员在他们身上开发应用程序。
有些事情我做错了。或者它完全是Twitter问题。
import java.io.ByteArrayInputStream;
import java.io.DataInputStream; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List;
import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXBuilder;
public class 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)