/** * Common backend for {@link #bulkShow(List)} and {@link #bulkShowById(List)}. Works in batches of * 100. * * <p>This will throw exceptions from the 1st page of results, but swallow them from subsequent * pages (which are likely to be rate limit errors). * * <p>Suspended bot accounts seem to just get ignored. * * @param stringOrNumber * @param screenNamesOrIds */ List<User> bulkShow2(String apiMethod, Class stringOrNumber, Collection screenNamesOrIds) { // Requires authentication in v1.1, though not in 1 which is still usable boolean auth = InternalUtils.authoriseIn11(jtwit); int batchSize = 100; ArrayList<User> users = new ArrayList<User>(screenNamesOrIds.size()); List _screenNamesOrIds = screenNamesOrIds instanceof List ? (List) screenNamesOrIds : new ArrayList(screenNamesOrIds); for (int i = 0; i < _screenNamesOrIds.size(); i += batchSize) { int last = i + batchSize; String names = InternalUtils.join(_screenNamesOrIds, i, last); String var = stringOrNumber == String.class ? "screen_name" : "user_id"; Map<String, String> vars = InternalUtils.asMap(var, names); try { String json = http.getPage(jtwit.TWITTER_URL + apiMethod, vars, auth); List<User> usersi = User.getUsers(json); users.addAll(usersi); } catch (TwitterException.E404 e) { // All names were bogus or deleted users! // Oh well } catch (TwitterException e) { // Stop here. // Don't normally throw an exception so we don't waste the // results we have. if (users.size() == 0) throw e; e.printStackTrace(); break; } } return users; }
// Called to initiate the background activity @Override protected String doInBackground(String... statuses) { try { Twitter.Status status = getTwitter().updateStatus(statuses[0]); return status.text; } catch (TwitterException e) { Log.e(TAG, e.toString()); e.printStackTrace(); return "Failed to post"; } }
/** * @return true if followerScreenName <i>is</i> following followedScreenName * @throws TwitterException.E403 if one of the users has protected their updates and you don't * have access. This can be counter-intuitive (and annoying) at times! Also throws E403 if one * of the users has been suspended (we use the {@link SuspendedUser} exception sub-class for * this). * @throws TwitterException.E404 if one of the users does not exist */ public boolean isFollower(String followerScreenName, String followedScreenName) { assert followerScreenName != null && followedScreenName != null; try { Map vars = InternalUtils.asMap( "source_screen_name", followerScreenName, "target_screen_name", followedScreenName); String page = http.getPage(jtwit.TWITTER_URL + "/friendships/show.json", vars, http.canAuthenticate()); JSONObject jo = new JSONObject(page); JSONObject trgt = jo.getJSONObject("relationship").getJSONObject("target"); boolean fby = trgt.getBoolean("followed_by"); return fby; } catch (TwitterException.E403 e) { if (e instanceof SuspendedUser) throw e; // Should this be a suspended user exception instead? // Let's ask Twitter // TODO check rate limits - only do if we have spare capacity String whoFirst = followedScreenName.equals(jtwit.getScreenName()) ? followerScreenName : followedScreenName; try { // this could throw a SuspendedUser exception show(whoFirst); String whoSecond = whoFirst.equals(followedScreenName) ? followerScreenName : followedScreenName; if (whoSecond.equals(jtwit.getScreenName())) throw e; show(whoSecond); } catch (TwitterException.RateLimit e2) { // ignore } // both shows worked? throw e; } catch (TwitterException e) { // FIXME investigating a weird new bug if (e.getMessage() != null && e.getMessage().contains("Two user ids or screen_names must be supplied")) throw new TwitterException( "WTF? inputs: follower=" + followerScreenName + ", followed=" + followedScreenName + ", call-by=" + jtwit.getScreenName() + "; " + e.getMessage()); throw e; } }
@Override protected String doInBackground(String... params) { try { twitter = new Twitter("horgag", "Hoggy22222"); twitter.setAPIRootUrl( "http://ec2-54-200-104-161.us-west-2.compute.amazonaws.com/statusnet/index.php/api"); twitter.setStatus(params[0]); Log.e(tag, "Posted: " + params[0]); return "Posted Upate: " + params[0]; } catch (TwitterException e) { Log.e(tag, "FAILED", e); e.printStackTrace(); return "Update not posted: " + params[0]; } }
/** * Destroy: Discontinues friendship with the user specified in the ID parameter as the * authenticating user. * * @param username The screen name of the user with whom to discontinue friendship. * @return the un-friended user (if they were a friend), or null if the method fails because the * specified user was not a friend. */ public User stopFollowing(String username) { String page; try { Map<String, String> vars = InternalUtils.asMap("screen_name", username); page = jtwit.http.post(jtwit.TWITTER_URL + "/friendships/destroy.json", vars, true); // ?? is this needed to make Twitter update its cache? doesn't seem // to fix things // http.getPage(jtwit.TWITTER_URL+"/friends", null, true); } catch (TwitterException e) { // were they a friend anyway? if (e.getMessage() != null && e.getMessage().contains("not friends")) return null; // Something else went wrong throw e; } // outside the try-catch block in case there is a json exception try { User user = new User(new JSONObject(page), null); return user; } catch (JSONException e) { throw new TwitterException.Parsing(page, e); } }