简体   繁体   中英

Fetch Twitter feed on Android - rate limit exceeded on device?

I'm trying to do a simple parse of a Twitter feed, I'm simply requesting the json feed of a user on twitter and parsing it into an object.

The problem is that this works perfect on the emulator but when deployed on a real device I get a 400 bad request response which seems to be a 403 rate limit exceeded, however this is the first request sent so how can the rate limit be exceeded?

      public ArrayList<Tweet> getTweets() {

      ArrayList<Tweet> tweets =
            new ArrayList<Tweet>();

      HttpClient client = new  DefaultHttpClient();
      HttpGet get = new HttpGet(twitterUrl);

      ResponseHandler<String> responseHandler =
            new BasicResponseHandler();

      String responseBody = null; 
      try {
        responseBody = client.execute(get, responseHandler);
      } catch(Exception ex) {
        ex.printStackTrace();
        timeOut = true;
      }

      JSONObject jsonObject = null;
    JSONArray arr = null;
    try {
        arr = new JSONArray(responseBody);
    } catch (JSONException e1) {
        e1.printStackTrace();
        timeOut = true;
    }

      for (int i = 0; i < arr.length(); i++) {
            try {
                jsonObject = arr.getJSONObject(i);
            } catch (JSONException e1) {
                e1.printStackTrace();
                timeOut = false;
            }
            Tweet tweet = null;
            try {
                tweet = new Tweet(
                      jsonObject.getJSONObject("user").getString("name"),
                      jsonObject.getString("text"),
                      jsonObject.getJSONObject("user").getString("profile_image_url"),
                      twitterHumanFriendlyDate(jsonObject.getString("created_at"))
                );
            } catch (JSONException e) {
                e.printStackTrace();
                timeOut = true;
            }
            tweets.add(tweet);
        }

      return tweets;
    }   

Here is an example of a feed that is parsed:

Example Twitter JSON feed

Am I missing something I have to do to get it to work on the device? I haven't registered with Titter or anything, do I have to get some sort of Twiiter key akin to a Google maps key or something?

EDIT: It actually works over Wi-Fi but not over mobile data connections, is their anyway to protect against rate limits on mobile connections?

This was a network issue on my carrier! Sorry!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM