Created
October 19, 2012 18:47
-
-
Save DHuckaby/3919951 to your computer and use it in GitHub Desktop.
TwitLonger utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TwitLongerHelper { | |
private static final String UTF_8 = "UTF-8"; | |
private String api_key; | |
private String application; | |
private String username; | |
public TwitLongerHelper(String api_key, String application, String username) { | |
this.api_key = api_key; | |
this.application = application; | |
this.username = username; | |
} | |
public TwitLongerResponse post(String message) { | |
TwitLongerResponse response = null; | |
try { | |
HttpPost httpPost = new HttpPost("http://www.twitlonger.com/api_post"); | |
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(); | |
nameValuePairList.add(new BasicNameValuePair("api_key", api_key)); | |
nameValuePairList.add(new BasicNameValuePair("application", application)); | |
nameValuePairList.add(new BasicNameValuePair("username", username)); | |
nameValuePairList.add(new BasicNameValuePair("message", message)); | |
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairList, UTF_8)); | |
String xml = EntityUtils.toString(new DefaultHttpClient().execute(httpPost).getEntity(), UTF_8); | |
response = new TwitLongerResponse(xml); | |
} catch (ClientProtocolException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return response; | |
} | |
public TwitLongerResponse setId(String message_id, String twitter_id) { | |
TwitLongerResponse response = null; | |
try { | |
HttpPost httpPost = new HttpPost("http://www.twitlonger.com/api_set_id"); | |
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(); | |
nameValuePairList.add(new BasicNameValuePair("api_key", api_key)); | |
nameValuePairList.add(new BasicNameValuePair("application", application)); | |
nameValuePairList.add(new BasicNameValuePair("message_id", message_id)); | |
nameValuePairList.add(new BasicNameValuePair("twitter_id", twitter_id)); | |
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairList, UTF_8)); | |
String xml = EntityUtils.toString(new DefaultHttpClient().execute(httpPost).getEntity(), UTF_8); | |
response = new TwitLongerResponse(xml); | |
} catch (ClientProtocolException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return response; | |
} | |
public static String get(String url) { | |
String response = null; | |
try { | |
HttpGet httpGet = new HttpGet("http://api.embed.ly/1/oembed?format=json&url=" + url); | |
String jsonString = EntityUtils.toString(new DefaultHttpClient().execute(httpGet).getEntity(), UTF_8); | |
response = new JSONObject(jsonString).getString("description"); | |
} catch (ClientProtocolException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
return response; | |
} | |
public static class TwitLongerResponse { | |
private String content; | |
private String error; | |
private String id; | |
private String link; | |
private String shortLink; | |
public TwitLongerResponse(String source) { | |
this.content = getTag("content", source); | |
this.error = getTag("error", source); | |
this.id = getTag("id", source); | |
this.link = getTag("link", source); | |
this.shortLink = getTag("short", source); | |
} | |
public String getContent() { | |
return content + ""; | |
} | |
public String getError() { | |
return error + ""; | |
} | |
public String getId() { | |
return id + ""; | |
} | |
public String getLink() { | |
return link + ""; | |
} | |
public String getShort() { | |
return shortLink + ""; | |
} | |
public boolean isError() { | |
return error != null; | |
} | |
private String getTag(String tag, String source) { | |
try { | |
XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); | |
factory.setNamespaceAware(true); | |
XmlPullParser xmlPullParser = factory.newPullParser(); | |
xmlPullParser.setInput(new StringReader(source)); | |
xmlPullParser.next(); | |
int eventType = xmlPullParser.getEventType(); | |
String content = null; | |
boolean shouldRead = false; | |
while (eventType != XmlPullParser.END_DOCUMENT) { | |
switch (eventType) { | |
case XmlPullParser.START_TAG: | |
if (xmlPullParser.getName().equals(tag)) { | |
shouldRead = true; | |
} | |
break; | |
case XmlPullParser.TEXT: | |
if (shouldRead) { | |
content = xmlPullParser.getText(); | |
} | |
break; | |
case XmlPullParser.END_TAG: | |
shouldRead = false; | |
break; | |
} | |
eventType = xmlPullParser.next(); | |
} | |
return content; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (XmlPullParserException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment