Tuesday 3 January 2012

Africa Android Challenge ACC 2012 Announced!

The Africa Android Challenge ACC 2012 has been announced:  http://androidchallenge.org/

I will be submitting an Adroid application "for social good" and will use this post to communicate my Android lab progress...

6 comments:

  1. Now just started with some "hello world" type of application. The application will simply do an HTTP GET request to the twitter search API and will display the "tweets" on the user's view.
    Once I get this very basic functionality done, I will change it to best align to my intended use of Android App which is the client-app for my Mobicents JAIN SLEE based service.

    ReplyDelete
  2. package org.ptagtug.ishmael.androidlab;

    /**
    * A simple Android Application that uses the AsyncHttpClient
    * (http://loopj.com/android-async-http/) to anonymously read tweets from Twitter using the Twitter Search API.
    * Only the last tweet from the read stream is displayed on the UI. Ideally, you'd want to display in the way that the user can scroll down and see the tweet-feeds.
    *
    * It uses the Gson JSON Parser (com.google.gson)
    *
    * NB: THIS CODE HAS NOT BEEN TESTED ON A REAL DEVICE
    Please contact the author is your were able to run this successfully on any device
    *
    * Author: Ishmael Makitla
    * Organizer/Member: Pretoria-GTUG (Google Technology User Group)
    * Country: South Africa
    */

    import java.util.Iterator;
    import java.util.Map.Entry;

    import com.loopj.android.http.AsyncHttpClient;
    import com.loopj.android.http.AsyncHttpResponseHandler;

    import android.app.Activity;
    import android.content.res.Resources.NotFoundException;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.ListView;
    import android.widget.TextView;
    import com.google.gson.*;

    public class TweetsReaderActivity extends Activity {
    private String url = null;
    private ListView tweetsView = null;
    private TextView tweetTextView = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // TODO Auto-generated method stub
    setContentView(R.layout.tweetsview);

    try{
    url = this.getResources().getString(R.string.defaultRemoteURL);
    tweetTextView = (TextView) findViewById(R.id.lastTweetText);
    }catch(NotFoundException nfe){
    Log.e(TweetsReaderActivity.class.getSimpleName(), " ERROR HERE: this.getResources().getString(R.string.defaultRemoteURL)");
    }
    }

    @Override
    protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    Log.d(TweetsReaderActivity.class.getSimpleName(),"onStart()");

    //getTweets(String twitterAPIURI);
    if(url != null) {
    Log.d(TweetsReaderActivity.class.getSimpleName(),"Loading Tweets from: "+url);
    getTweets(url);
    }
    else {
    Log.w(TweetsReaderActivity.class.getSimpleName(), "URL Not Set");
    }
    }

    private void getTweets(String url){
    Log.d(TweetsReaderActivity.class.getSimpleName(),"getTweets");
    AsyncHttpClient httpClient = new AsyncHttpClient();
    AsyncHttpResponseHandler handler = new AsyncHttpResponseHandler(){@Override public void onSuccess(String response){processTweets(response);}};
    httpClient.get(url, handler);
    }

    }

    ReplyDelete
  3. private void processTweets(String tweetsStream){
    Log.d(TweetsReaderActivity.class.getSimpleName(),"processTweets: \n"+tweetsStream);
    try{ gsonProcessTweets(tweetsStream); } catch(JsonParseException jpe){jpe.printStackTrace();}
    }
    /**
    * Code from Ishmael Makitla's JAIN SLEE lab project
    * THIS CODE HAS NOT BEEN TESTED ON REAL DEVICE
    */
    private void gsonProcessTweets(String jsonStringOfTweets) throws JsonParseException{
    JsonParser parser = new JsonParser();
    Log.d(TweetsReaderActivity.class.getSimpleName(),"calling JsonParser.parse(...): \n");
    JsonElement jelem = parser.parse(jsonStringOfTweets);

    String lastTweetDetails = "";

    if(jelem.isJsonObject()){
    Log.d(TweetsReaderActivity.class.getSimpleName(),"Is Json-Object");
    Iterator> elemIterator = jelem.getAsJsonObject().entrySet().iterator();
    while(elemIterator.hasNext()){
    Entry entry = elemIterator.next();
    if(entry.getKey().equalsIgnoreCase("results")){
    Log.d(TweetsReaderActivity.class.getSimpleName(),"Tweets-Result = "+entry.getValue());

    if(entry.getValue().isJsonArray()) {
    //result
    Iterator it = entry.getValue().getAsJsonArray().iterator();
    //iterate through tweet-results created_at's
    String tweetDetails = "";
    while(it.hasNext()){
    //a created_at value
    JsonElement je = it.next();

    if(je.isJsonObject()){
    //tracer.info("Location: = "+je.getAsJsonObject().get("location")+" ||==|| "+je.getAsJsonObject().get("text"));
    String user = (je.getAsJsonObject().get("from_user")!=null? je.getAsJsonObject().get("from_user").toString().replaceAll("\"", ""):"");

    //Tweet text
    String tweetText = (je.getAsJsonObject().get("text") !=null?je.getAsJsonObject().get("text").toString().replaceAll("\"", ""):"");

    //Location
    String locationString = (je.getAsJsonObject().get("location")!=null? je.getAsJsonObject().get("location").toString().replaceAll("\"", ""):"");
    String locIndex = "ÜT:";
    String tempCoordinates = "";
    if(locationString.contains(locIndex)){
    tempCoordinates = locationString.substring(locationString.indexOf(locIndex)+locIndex.length()+1);
    tempCoordinates = tempCoordinates.replaceAll("\"", "");
    Log.d(TweetsReaderActivity.class.getSimpleName(),"Location Coordinates: "+tempCoordinates);
    if(tempCoordinates.indexOf(",") != -1){
    String[] xy = tempCoordinates.trim().split(",");
    double y =Double.valueOf(xy[1]);
    double x = Double.valueOf(xy[0]);
    Log.d(TweetsReaderActivity.class.getSimpleName(),"Lat : "+y+" Long: "+x);
    }
    }
    //full tweet:
    tweetDetails= "Sender: "+user+" \nLocation:"+locationString+" \nText:"+tweetText;
    Log.d(TweetsReaderActivity.class.getSimpleName(),"Full Tweet Details :\n"+tweetDetails);
    }
    }
    //last tweet:
    lastTweetDetails = tweetDetails;
    }
    }
    }
    }
    //display the last tweet on screen
    Log.d(TweetsReaderActivity.class.getSimpleName(),"Last Tweet Details :\n"+lastTweetDetails);
    if(tweetTextView !=null)
    tweetTextView.setText((lastTweetDetails==null?"No-Tweet":lastTweetDetails));
    }

    ReplyDelete
  4. Make sure you have added the following permission in your AndroidManifest.xml file: uses-permission android:name="android.permission.INTERNET"
    This is required to make TCP Socket connections such as HTTP (sending a GET request to twitter in the case of this post)

    ReplyDelete
  5. Oh, and the value of the URL is http://search.twitter.com/search.json?geocode=-25.73063,28.21289,3km

    ReplyDelete
  6. package org.ptagtug.ishmael.androidlab;

    /**
    * This is a simple Adnroid Activity that allows the user to click on the button to get tweets from twitter
    * on a separate Activity (see TweetsReaderActivity.class). When a user clicks on the button, a "intent" is
    * sent to start reading tweets anonymously.
    *
    * Author: Ishmael Makitla
    * Organizer/Member: Pretoria-GTUG (Google Technology User Group)
    * Country: South Africa
    */

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class IshmaelAndroidLabDemoActivity extends Activity {
    private String TAG = IshmaelAndroidLabDemoActivity.class.getSimpleName();
    private Button btnViewTweets = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate() - method invoked");
    setContentView(R.layout.main);
    btnViewTweets = (Button) findViewById(R.id.button1);
    }

    @Override
    protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    btnViewTweets.setOnClickListener(tweetsViewButtonClickListener);
    }

    /**
    * Anonymous implementation of the Button-onClick listener
    */
    private OnClickListener tweetsViewButtonClickListener = new OnClickListener() {
    public void onClick(View view) {
    Intent meinIntent = new Intent(getBaseContext(), TweetsReaderActivity.class);
    startActivity(meinIntent);
    }
    };
    }

    ReplyDelete