Skip to content

Commit

Permalink
Merge pull request #1 from douglascraigschmidt/master
Browse files Browse the repository at this point in the history
abc
  • Loading branch information
fshams committed Jul 8, 2014
2 parents 8f5710e + 0e0f0f9 commit 71a9a88
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 138 deletions.
48 changes: 22 additions & 26 deletions ex/AcronymApplication/src/edu/vuum/mocca/AcronymActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import android.os.AsyncTask;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
Expand All @@ -28,7 +29,7 @@ public class AcronymActivity extends AcronymActivityBase {
* return the results back to the DownloadActivity.
*/
@Override
public void sendResults(final List<AcronymData> acronymDataList)
public void sendResults(final List<AcronymData> acronymDataList)
throws RemoteException {
// Since the Android Binder framework dispatches this
// method in a separate Thread we need to explicitly
Expand Down Expand Up @@ -100,32 +101,27 @@ public void expandAcronymSync(View v) {

hideKeyboard();

// Use mAcronymCall to download the Acronym data in a
// separate Thread and then display it in the UI Thread.
// We use a separate Thread to avoid blocking the UI
// Thread.
new Thread(new Runnable() {
public void run () {
try {
Log.d(TAG,
"Calling twoway AcronymServiceSync.expandAcronym()");

// Download the expanded acronym via a
// synchronous two-way method call.
final List<AcronymData> acronymDataList =
acronymCall.expandAcronym(acronym);

// Display the results in the UI Thread.
runOnUiThread(new Runnable() {
public void run() {
displayResults(acronymDataList);
}
});
} catch (RemoteException e1) {
e1.printStackTrace();
}
// Use an AsyncTask to download the Acronym data in a
// separate thread and then display it in the UI thread.
new AsyncTask<String, Void, List<AcronymData>> () {

// Download the expanded acronym via a synchronous
// two-way method call, which runs in a background
// thread to avoid blocking the UI thread.
protected List<AcronymData> doInBackground(String... acronyms) {
try {
return acronymCall.expandAcronym(acronyms[0]);
} catch (RemoteException e) {
e.printStackTrace();
}
}).start();
return null;
}

// Display the results in the UI Thread.
protected void onPostExecute(List<AcronymData> acronymDataList) {
displayResults(acronymDataList);
}
}.execute(acronym);
} else {
Log.d(TAG, "mAcronymCall was null.");
}
Expand Down
29 changes: 13 additions & 16 deletions ex/AcronymApplication/src/edu/vuum/mocca/AcronymData.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package edu.vuum.mocca;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

Expand Down Expand Up @@ -31,29 +28,19 @@ public class AcronymData implements Parcelable {
/**
* The long form of the acronym (spelled out version).
*/
String mLongForm;
public String mLongForm;

/**
* The relative frequency of usage in print, of this meaning of
* the acronym.
*/
int mFreq;
public int mFreq;

/**
* The year the acronym was added to this database of acronyms, or
* was originally termed.
*/
int mSince;

/**
* Constructor that takes a JSON Object to construct the object
* (in the JSON format that the Acronym website provides).
*/
public AcronymData(JSONObject jsonObject) throws JSONException {
mLongForm = jsonObject.getString("lf");
mFreq = jsonObject.getInt("freq");
mSince = jsonObject.getInt("since");
}
public int mSince;

/**
* Private constructor provided for the CREATOR interface, which
Expand All @@ -65,6 +52,16 @@ private AcronymData(Parcel in) {
mSince = in.readInt();
}

/**
* Constructor that initializes an AcronymData object from
* its parameters.
*/
public AcronymData(String longForm, int freq, int since) {
mLongForm = longForm;
mFreq = freq;
mSince = since;
}

/**
* The toString() custom implementation.
*/
Expand Down
160 changes: 77 additions & 83 deletions ex/AcronymApplication/src/edu/vuum/mocca/AcronymDownloadUtils.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
package edu.vuum.mocca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.net.http.AndroidHttpClient;
import android.util.Log;

/**
* A class to handle the Actual Downloading of information from the
* Internet.
* @class AcronymDownloadUtils
*
* @brief Handles the actual downloading of Acronym information from
* the GeoNames Web service.
*/
public class AcronymDownloadUtils {
// Logging Tag
private final static String LOG_TAG = AcronymDownloadUtils.class
private final static String TAG = AcronymDownloadUtils.class
.getCanonicalName();

/**
Expand All @@ -31,90 +35,80 @@ public class AcronymDownloadUtils {
"http://www.nactem.ac.uk/software/acromine/dictionary.py?sf=";

/**
* Obtain the Acronym information.
*
* @return The information that responds to your current acronym search.
* A Handler that encapsulates the process of generating a
* response object from a HttpResponse.
*/
public static List<AcronymData> getResults(final String acronym) {
// Create the return value, which is a list of AcronymData
// objects.
final List<AcronymData> rValue =
new ArrayList<AcronymData>();

// Try to access the Internet and parse the JSON reply.
try {
// Go the URL and read the JSON array from it, using the
// readJsonArrayFromUrl() helper method.
final JSONArray obj = readJsonArrayFromUrl(ACRONYM_URL + acronym);

// Get the JSON array of results, marked with the 'lfs'
// name.
final JSONArray options = obj.getJSONObject(0).getJSONArray("lfs");

// For each option, create an AcronymData and add it to
// rValue.
for (int i = 0; i < options.length(); i++) {
rValue.add(new AcronymData(options.getJSONObject(i)));
static class JSONResponseHandler implements ResponseHandler<List<AcronymData>> {
/**
* This factory method converts the HttpResponse received from
* the GeoNames Web service into a List of AcronymData.
*/
@Override
public List<AcronymData> handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {

// Stores the processed results we get back from the
// GeoNames Web service.
List<AcronymData> result = new ArrayList<AcronymData>();

// A ResponseHandler that returns the response body as a
// String for successful (2xx) responses.
String JSONResponse =
new BasicResponseHandler().handleResponse(response);

try {
// Takes a JSON source string and extracts characters
// and tokens from it.
JSONArray object =
(JSONArray) new JSONTokener(JSONResponse).nextValue();

// Get the JSON array of acronym results, marked with
// the 'lfs' name.
final JSONArray options = object.getJSONObject(0).getJSONArray("lfs");

// For each option, create an AcronymData and add it
// to rValue.
for (int i = 0; i < options.length(); i++) {
JSONObject jsonObject = options.getJSONObject(i);
result.add(new AcronymData(jsonObject.getString("lf"),
jsonObject.getInt("freq"),
jsonObject.getInt("since")));
}
} catch (JSONException e) {
e.printStackTrace();
}

// Log for debugging purposes.
Log.d(LOG_TAG, "" + obj.getJSONObject(0).getJSONArray("lfs").length());

} catch (JSONException e) {
Log.e(LOG_TAG, "JSONException: " + e.getMessage());
} catch (IOException e) {
Log.e(LOG_TAG, "IOException: " + e.getMessage());
return result;
}

return rValue;
}

/**
* Helper method that reads a URL and returns a JSONArray of the
* data, which only works on a URL to a Web service that returns
* JSON in an array format.
* Obtain the Acronym information.
*
* No defensive programming in this exmaple, for brevity, in
* production code you would do a lot more checks, etc.
* @return The information that responds to your current acronym search.
*/
public static JSONArray readJsonArrayFromUrl(String url)
throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
// Create a BufferedReader containing the contents
// downloaded from the Acronym Web service.
BufferedReader rd =
new BufferedReader(new InputStreamReader
(is,
Charset.forName("UTF-8")));

// Convert the java.io.Reader parameter to a String.
String jsonText = readAll(rd);
public static List<AcronymData> getResults(AndroidHttpClient client,
final String acronym) {
// Object that encapsulates the HTTP GET request to the
// Acronym Web service.
HttpGet request = new HttpGet(ACRONYM_URL + acronym);

// Create a JSONarray from the String.
return new JSONArray(jsonText);
} finally {
is.close();
}
}
// Factory method that converts the JSON string returned from
// the Acronym Web service into a List of AcronymData.
JSONResponseHandler responseHandler =
new JSONResponseHandler();

/**
* Convert the java.io.Reader parameter to a String.
*
* Helper Method for readJsonArrayFromUrl.
*
* @param rd
* java.io.Reader to read from
*
* @return String composed of the characters from the Reader.
*/
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
try {
// Get expanded acronyms from the Web service in JSON
// format, parse data into a List of AcronymData, and
// return the results.
return client.execute(request,
responseHandler);
} catch (ClientProtocolException e) {
Log.i(TAG, "ClientProtocolException");
} catch (IOException e) {
Log.i(TAG, "IOException");
}
return sb.toString();
return null;

}

}
19 changes: 16 additions & 3 deletions ex/AcronymApplication/src/edu/vuum/mocca/AcronymServiceAsync.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.http.AndroidHttpClient;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
Expand Down Expand Up @@ -35,6 +36,12 @@ public class AcronymServiceAsync extends Service {
private final static String TAG =
AcronymServiceAsync.class.getCanonicalName();

/**
* Object that can invoke HTTP GET requests on URLs.
*/
private final static AndroidHttpClient mClient =
AndroidHttpClient.newInstance("");

/**
* Called when a client (e.g., AcronymActivity) calls
* bindService() with the proper Intent. Returns the
Expand Down Expand Up @@ -77,14 +84,15 @@ public static Intent makeIntent(Context context) {
* callback.
*/
@Override
public void expandAcronym(AcronymResults callback,
String acronym)
public void expandAcronym(AcronymResults callback,
String acronym)
throws RemoteException {

// Call the Acronym Web service to get the list of
// possible expansions of the designated acronym.
List<AcronymData> acronymResults =
AcronymDownloadUtils.getResults(acronym);
AcronymDownloadUtils.getResults(mClient,
acronym);

Log.d(TAG, "" + acronymResults.size() + " results for acronym: " + acronym);

Expand All @@ -93,4 +101,9 @@ public void expandAcronym(AcronymResults callback,
callback.sendResults(acronymResults);
}
};

public void onDestroy() {
mClient.close();
super.onDestroy();
}
}
Loading

0 comments on commit 71a9a88

Please sign in to comment.