在点击 url 时随机获取 HTTP 400 以获取 Twitter 关注者/朋友 - java

发布于 2024-09-09 03:05:53 字数 2810 浏览 3 评论 0原文

我正在 Twitter 上开发一个应用程序。

Twitter 随机给我回复 400。我很恐慌。他们的服务器太糟糕了,无法满足请求。我正在点击他们的 REST URL 以通过光标获取关注者。有时我会获得前 100 个关注者,而在第二次游标迭代时我会得到 HTTP 400。

只有一次尝试就获得了 300 个关注者。否则一切都会以 HTTP 代码 400 结束。我正在使用递归。基于 next_cursor 的值,直到它达到 0。我继续用它的值点击 URL 以获取下一页的关注者。

你们有什么建议?如何克服这种情况。推特服务器回复不好的地方。他们在邀请开发者在其上开发应用程序方面犯了一个错误。

我做错了什么吗?或者完全是 Twitter 的问题。

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)

I am developing an application on Twitter.

And Twitter is giving me response of 400 on random times. I am in panic. Their servers are too bad to entertain requests. I am hitting their REST URL to get followers with cursor. Some times I get first 100 followers and on second cursor iteration I got HTTP 400.

Only one attempt gave me my 300 followers. Otherwise all ends in HTTP Code 400. I am using recursion. Based upon the value of next_cursor, till it reaches 0. I keep on hitting the URL with its value to get next page of followers.

What you guys suggest? How to overcome this situation. Where twitter servers are bad in replying. And they made a mistake to invite developers to develop apps on them.

Is some thing I am doing wrong. Or its totally Twitter issue.

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文