views:

23

answers:

0

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)