diff options
| author | koem <koem@petoria.de> | 2013-07-15 14:31:01 +0200 |
|---|---|---|
| committer | koem <koem@petoria.de> | 2013-07-15 14:35:41 +0200 |
| commit | e9940edc413c505da723c6cd43a1bfdb36241679 (patch) | |
| tree | d287c66c525850577e8c07c045ddbaf1efe4c81d | |
| parent | 6b031d735b4277effd12257c94bc5c2a101c0bfe (diff) | |
| download | cgeo-e9940edc413c505da723c6cd43a1bfdb36241679.zip cgeo-e9940edc413c505da723c6cd43a1bfdb36241679.tar.gz cgeo-e9940edc413c505da723c6cd43a1bfdb36241679.tar.bz2 | |
Fixes #3002 - Tweet not tweeted
Twitter api v1.0 seems to be deprecated for tweeting (Auth worked fine). Now using v1.1.
| -rw-r--r-- | main/src/cgeo/geocaching/twitter/Twitter.java | 72 |
1 files changed, 35 insertions, 37 deletions
diff --git a/main/src/cgeo/geocaching/twitter/Twitter.java b/main/src/cgeo/geocaching/twitter/Twitter.java index a6ce7d2..be37d33 100644 --- a/main/src/cgeo/geocaching/twitter/Twitter.java +++ b/main/src/cgeo/geocaching/twitter/Twitter.java @@ -1,7 +1,6 @@ package cgeo.geocaching.twitter; import cgeo.geocaching.Geocache; -import cgeo.geocaching.settings.Settings; import cgeo.geocaching.Trackable; import cgeo.geocaching.cgData; import cgeo.geocaching.cgeoapplication; @@ -11,6 +10,7 @@ import cgeo.geocaching.geopoint.GeopointFormatter.Format; import cgeo.geocaching.network.Network; import cgeo.geocaching.network.OAuth; import cgeo.geocaching.network.Parameters; +import cgeo.geocaching.settings.Settings; import cgeo.geocaching.utils.Log; import ch.boye.httpclientandroidlib.HttpResponse; @@ -19,13 +19,20 @@ import org.apache.commons.lang3.StringUtils; public final class Twitter { private static final String HASH_PREFIX_WITH_BLANK = " #"; - public static final int MAX_TWEET_SIZE = 140; + private static final int MAX_TWEET_SIZE = 140; - public static void postTweet(final cgeoapplication app, final String status, final Geopoint coords) { - if (app == null) { - return; - } - if (!Settings.isTwitterLoginValid()) { + public static void postTweetCache(String geocode) { + final Geocache cache = cgData.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB); + postTweet(cgeoapplication.getInstance(), getStatusMessage(cache), null); + } + + public static void postTweetTrackable(String geocode) { + final Trackable trackable = cgData.loadTrackable(geocode); + postTweet(cgeoapplication.getInstance(), getStatusMessage(trackable), null); + } + + private static void postTweet(final cgeoapplication app, final String status, final Geopoint coords) { + if (app == null || !Settings.isUseTwitter() || !Settings.isTwitterLoginValid()) { return; } @@ -38,19 +45,23 @@ public final class Twitter { "display_coordinates", "true"); } - OAuth.signOAuth("api.twitter.com", "/1/statuses/update.json", "POST", false, parameters, Settings.getTokenPublic(), Settings.getTokenSecret(), Settings.getKeyConsumerPublic(), Settings.getKeyConsumerSecret()); - final HttpResponse httpResponse = Network.postRequest("http://api.twitter.com/1/statuses/update.json", parameters); - if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == 200) { - Log.i("Tweet posted"); + OAuth.signOAuth("api.twitter.com", "/1.1/statuses/update.json", "POST", false, parameters, Settings.getTokenPublic(), Settings.getTokenSecret(), Settings.getKeyConsumerPublic(), Settings.getKeyConsumerSecret()); + final HttpResponse httpResponse = Network.postRequest("http://api.twitter.com/1.1/statuses/update.json", parameters); + if (httpResponse != null) { + if (httpResponse.getStatusLine().getStatusCode() == 200) { + Log.i("Tweet posted"); + } else { + Log.e("Tweet could not be posted. Reason: " + httpResponse.toString()); + } } else { - Log.e("Tweet could not be posted"); + Log.e("Tweet could not be posted. Reason: httpResponse Object is null"); } } catch (Exception e) { Log.e("Twitter.postTweet", e); } } - public static void appendHashTag(final StringBuilder status, final String tag) { + private static void appendHashTag(final StringBuilder status, final String tag) { if (status.length() + HASH_PREFIX_WITH_BLANK.length() + tag.length() <= MAX_TWEET_SIZE) { final String tagWithPrefix = HASH_PREFIX_WITH_BLANK + tag; if (status.indexOf(tagWithPrefix, 0) == -1) { @@ -59,15 +70,7 @@ public final class Twitter { } } - public static void postTweetCache(String geocode) { - if (!Settings.isUseTwitter() || !Settings.isTwitterLoginValid()) { - return; - } - final Geocache cache = cgData.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB); - postTweet(cgeoapplication.getInstance(), getStatusMessage(cache), null); - } - - static String getStatusMessage(Geocache cache) { + private static String getStatusMessage(Geocache cache) { String name = cache.getName(); if (name.length() > 100) { name = name.substring(0, 100) + '…'; @@ -76,21 +79,7 @@ public final class Twitter { return fillTemplate(Settings.getCacheTwitterMessage(), name, url); } - private static String fillTemplate(String template, String name, final String url) { - String result = StringUtils.replace(template, "[NAME]", name); - result = StringUtils.replace(result, "[URL]", url); - StringBuilder builder = new StringBuilder(result); - appendHashTag(builder, "cgeo"); - appendHashTag(builder, "geocaching"); - return builder.toString(); - } - - public static void postTweetTrackable(String geocode) { - final Trackable trackable = cgData.loadTrackable(geocode); - postTweet(cgeoapplication.getInstance(), getStatusMessage(trackable), null); - } - - static String getStatusMessage(Trackable trackable) { + private static String getStatusMessage(Trackable trackable) { String name = trackable.getName(); if (name.length() > 82) { name = name.substring(0, 81) + '…'; @@ -99,4 +88,13 @@ public final class Twitter { String status = Settings.getTrackableTwitterMessage(); return fillTemplate(status, name, url); } + + private static String fillTemplate(String template, String name, final String url) { + String result = StringUtils.replace(template, "[NAME]", name); + result = StringUtils.replace(result, "[URL]", url); + StringBuilder builder = new StringBuilder(result); + appendHashTag(builder, "cgeo"); + appendHashTag(builder, "geocaching"); + return builder.toString(); + } } |
