Created a simple declarative component, where a user can search for tweets (not fool proof, though).
This project has a single ADF view Controller project, where a new declarative component called Tweet Viewer has been created.The component has two layouts: one search region with a text box and other a panel header displaying all the tweets that resulted from Twitter API.
In the search region, a Value change listener is attached to the inputText.
Initially, thought of invoking the Twitter API via URLServiceDataControl,
but there were some issues while creating it with Twitter end point.
Hence on the value change listener of this component, invoked an HTTP request via HTTP core APIs and retrived the JSON response.
// Comment
HttpClient httpclient= new DefaultHttpClient();
String url = SEARCH_TWITTER+valueChangeEvent.getNewValue();
System.err.println(url);
HttpGet httpget = new HttpGet(url);
HttpResponse response;
try
{
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null)
{
InputStream instream = entity.getContent();
String result = convertStreamToString(instream);
instream.close();
System.out.println(result);
this.setJsonResponse(result);
populateTweets();
}
}
With JsonResponse in the context, invoked another API, which parses this JSON response into array of Tweet Objects.All the Tweets in this bean's array are displayed by iterating using af:forEach operation.
// Comment
private void populateTweets() {
ArrayList tweets1 = new ArrayList();
if(getJsonResponse()==null) return ;
try
{
JSONObject json = new JSONObject(this.getJsonResponse());
JSONArray jsonArray = (JSONArray) json.get("results");
int size = jsonArray.length();
for(int i=0;i<size;i++)
{
JSONObject obj = (JSONObject)jsonArray.get(i);
String tweetMessage= (String)obj.getString("text");
String from = (String)obj.getString("from_user_name");
String profileURL = "https://twitter.com/"+obj.getString("from_user");
Tweet tweet = new Tweet(tweetMessage,from,profileURL);
tweets1.add(tweet);
}
}
catch(JSONException ex)
{
}
setTweets(tweets1);
}
// Comment
Code: Download
No comments:
Post a Comment