Skip to content

Commit

Permalink
Now you can display stats from the Web API
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkingserious committed Jan 4, 2014
1 parent 2d6907e commit f8113af
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 23 deletions.
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry kind="lib" path="libs/sendgrid-0.1.2-jar.jar"/>
<classpathentry kind="lib" path="libs/json-simple-1.1.1.jar"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
Binary file added libs/json-simple-1.1.1.jar
Binary file not shown.
34 changes: 21 additions & 13 deletions res/layout/activity_display_message.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".DisplayMessageActivity" >

<TextView
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content">

<TextView android:id="@+id/response"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

</RelativeLayout>

<TextView android:id="@+id/statistics"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

</LinearLayout>
</ScrollView>
119 changes: 110 additions & 9 deletions src/com/thinkingserious/sendgrid/DisplayMessageActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
package com.thinkingserious.sendgrid;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutionException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.thinkingserious.sendgrid.R;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.MenuItem;
Expand All @@ -11,9 +25,71 @@
import android.content.Intent;
import android.os.Build;

import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.ParseException;
import org.json.simple.parser.JSONParser;

@SuppressLint("NewApi")
public class DisplayMessageActivity extends Activity {


// Get statistics from SendGrid using the Web API, returns a JSON object as described in the URL below
// http://sendgrid.com/docs/API_Reference/Web_API/Statistics/index.html
class StatsFromSendGrid extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
Utils creds = new Utils();
// Reference: http://sendgrid.com/docs/API_Reference/Web_API/Statistics/index.html
String url = "https://api.sendgrid.com/api/stats.get.json?api_user=" + creds.getUsername() + "&api_key=" + creds.getPassword();
// Prepare a request object
HttpGet httpget = new HttpGet(url);

// Execute the request
HttpResponse response = null;
String result = null;
try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream instream = entity.getContent();
result = convertStreamToString(instream);
// now you have the string representation of the HTML request
instream.close();
} catch (Exception e) {}
return result;
}

// Code taken from here: http://stackoverflow.com/questions/4457492/simple-http-client-example-in-android
private String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -30,18 +106,43 @@ protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

// TODO Parse the resulting JSON and display pretty printed messages
// TODO Add some data from the Statistics API
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(20);
textView.setText(message);
// Update the display with the response code from the mail end point
TextView testView = (TextView) findViewById(R.id.response);
message = "Email Send Status: " + message + "\n";
testView.setText(message);

// Set the text view as the activity layout
setContentView(textView);
// Get the stats from the SendGrid Web API
StatsFromSendGrid stats = new StatsFromSendGrid();
String result = null;
try {
result = stats.execute().get();
} catch (InterruptedException e) {
// TODO Implement exception handling
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Implement exception handling
e.printStackTrace();
}

// Make the returned JSON readable
JSONParser parser=new JSONParser();
try{
Object obj = parser.parse(result);
JSONArray array = (JSONArray)obj;
JSONObject obj2 = (JSONObject)array.get(0);
result = "Requests: " + obj2.get("requests").toString();
result = result + "\nDelivered: " + obj2.get("delivered").toString();
result = result + "\nOpens: " + obj2.get("opens").toString();
}catch(ParseException pe){
result = pe.toString();
}

// Update the display with data from the stats end point
testView = (TextView) findViewById(R.id.statistics);
testView.setText(result);

}

/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/com/thinkingserious/sendgrid/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected String doInBackground(Hashtable<String,String>... params) {
}
}

/** Called when the user clicks the Send button **/
// Called when the user clicks the Send button
@SuppressWarnings("unchecked")
public void sendMessage(View view) {
Hashtable<String,String> params = new Hashtable<String,String>();
Expand Down

0 comments on commit f8113af

Please sign in to comment.