Skip to content

Instantly share code, notes, and snippets.

@Red-Folder
Created September 24, 2012 20:28
Show Gist options
  • Save Red-Folder/3778161 to your computer and use it in GitHub Desktop.
Save Red-Folder/3778161 to your computer and use it in GitHub Desktop.
Phonegap Service Tutorial - Part 3 - TwitterService.java - newTweet
private Boolean newTweet() {
Boolean result = false;
String oldMaxID = getMaxID();
String newMaxID = oldMaxID;
HttpClient httpClient;
HttpGet getMethod;
HttpResponse response;
InputStream responseStream;
try {
httpClient = new DefaultHttpClient();
getMethod = new HttpGet("http://search.twitter.com/search.json?q=phonegap&rpp=1&page1");
response = httpClient.execute(getMethod);
responseStream = response.getEntity().getContent();
StringBuilder dataString = new StringBuilder();
char[] buffer = new char[1024];
Reader reader = new InputStreamReader(responseStream, "UTF-8");
int charCount;
while ((charCount = reader.read(buffer, 0, buffer.length)) > 0){
dataString.append(buffer, 0, charCount);
}
JSONObject data = new JSONObject(dataString.toString());
if (data.has("max_id_str")) {
newMaxID = data.getString("max_id_str");
if (!newMaxID.equals(oldMaxID)) {
setMaxID(newMaxID);
result = true;
}
}
} catch (Exception ex) {
// Do something with the error in production code
} finally {
// Close out the response stream and any open connections in production code
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment