Skip to content

Commit

Permalink
change api call to openlibrary
Browse files Browse the repository at this point in the history
  • Loading branch information
mluedke2 committed Oct 9, 2013
1 parent 76698f6 commit d34a940
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 75 deletions.
100 changes: 100 additions & 0 deletions OMGAndroid/src/main/java/com/example/omgandroid/JSONAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.example.omgandroid;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONObject;

public class JSONAdapter extends BaseAdapter {

Activity mActivity;
JSONArray mJsonArray;

public JSONAdapter(Activity activity) {
mActivity = activity;
mJsonArray = new JSONArray();
}

public void updateData(JSONObject jsonObject) {

// update the adapter's dataset
mJsonArray = jsonObject.optJSONArray("docs");
notifyDataSetChanged();
}

@Override public int getCount() {
return mJsonArray.length();
}

@Override public JSONObject getItem(int position) {
return mJsonArray.optJSONObject(position);
}

@Override public long getItemId(int position) {

// our particular dataset uses String IDs
// but we had to put something in this method
return 0;
}

@Override public View getView(int position, View convertView, ViewGroup parent) {

// if the view already exists, no need to inflate again!
if (convertView == null) {

// There's a standard list item in Android XML. We're going to inflate that.
convertView = mActivity.getLayoutInflater().inflate(R.layout.row_book, null);
}

// Initialize the three views we will be populating
ImageView thumbnailImageView = (ImageView) convertView.findViewById(R.id.img_thumbnail);
TextView titleTextView = (TextView) convertView.findViewById(R.id.text_title);
TextView authorTextView = (TextView) convertView.findViewById(R.id.text_author);

// Get the current book's data in JSON form
JSONObject jsonObject = getItem(position);

// See if there is a cover ID in the Object
if (jsonObject.has("cover_i")) {

// If so, grab the Cover ID out from the object
String imageID = jsonObject.optString("cover_i");

// and construct the image URL
String imageURL = "http://covers.openlibrary.org/b/id/"
+ jsonObject.optString("cover_i")
+ "-S.jpg";

// Use Picasso to load the image
// Use a placeholder in case the real cover is slow to load
Picasso.with(mActivity)
.load(imageURL)
.placeholder(R.drawable.ic_books)
.into(thumbnailImageView);
} else {

// If there was no cover ID, use a placeholder
thumbnailImageView.setImageResource(R.drawable.ic_books);
}

// Grab the title and author from the JSON
String bookTitle = jsonObject.optString("title");
String authorName = jsonObject.optString("author_name");

// the author comes back with extra characters, so let's remove those
authorName = authorName.replace("[", "").replace("\"", "").replace("]", "");

// Send these Strings to the TextViews for display
titleTextView.setText(bookTitle);
authorTextView.setText(authorName);

return convertView;
}
}

This file was deleted.

22 changes: 11 additions & 11 deletions OMGAndroid/src/main/java/com/example/omgandroid/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.json.JSONArray;
import org.json.JSONObject;

public class MainActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener {

Expand All @@ -27,7 +27,7 @@ public class MainActivity extends Activity implements View.OnClickListener, Adap
EditText mainEditText;
ListView mainListView;
ShareActionProvider mShareActionProvider;
JSONArrayAdapter mJSONArrayAdapter;
JSONAdapter mJSONAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -47,11 +47,11 @@ protected void onCreate(Bundle savedInstanceState) {
// Access the ListView
mainListView = (ListView) findViewById(R.id.main_listview);

// Create a JSONArrayAdapter for the ListView
mJSONArrayAdapter = new JSONArrayAdapter(this, new JSONArray());
// Create a JSONAdapter for the ListView
mJSONAdapter = new JSONAdapter(this);

// Set the ListView to use the ArrayAdapter
mainListView.setAdapter(mJSONArrayAdapter);
mainListView.setAdapter(mJSONAdapter);

// Set this activity to react to list items being pressed
mainListView.setOnItemClickListener(this);
Expand All @@ -60,20 +60,20 @@ protected void onCreate(Bundle savedInstanceState) {
AsyncHttpClient client = new AsyncHttpClient();

// Have the client get a JSONArray of data, and define how to respond
client.get("http://open.undp.org/api/donor-country-index.json", new JsonHttpResponseHandler() {
client.get("http://openlibrary.org/search.json?q=the+lord+of+the+rings", new JsonHttpResponseHandler() {

@Override
public void onSuccess(JSONArray jsonArray) {
public void onSuccess(JSONObject jsonObject) {

// Display a "Toast" message to announce our success
Toast.makeText(getApplicationContext(),
"Success!",
Toast.LENGTH_LONG)
.show();

// Now we are being wise and have created the JSONArrayAdapter subclass
// Now we are being wise and have created the JSONAdapter subclass
// update the data in our custom method.
mJSONArrayAdapter.updateData(jsonArray);
mJSONAdapter.updateData(jsonObject);
}

@Override
Expand Down Expand Up @@ -140,9 +140,9 @@ private void setShareIntent() {
// Log the item's position and contents to the console in Debug
Log.d("omg android", position
+ ": "
+ mJSONArrayAdapter.getItem(position).optString("name")
+ mJSONAdapter.getItem(position).optString("name")
+ " ("
+ mJSONArrayAdapter.getItem(position).optString("id")
+ mJSONAdapter.getItem(position).optString("id")
+ ")");
}
}
5 changes: 2 additions & 3 deletions OMGAndroid/src/main/res/layout/row_book.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
android:id="@+id/img_thumbnail"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignLeft="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="25dp"
android:layout_centerVertical="true"
android:scaleType="centerInside"
Expand All @@ -19,9 +19,8 @@
android:id="@+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/img_thumbnail"
android:layout_marginTop="25dp"
android:layout_alignTop="@+id/img_thumbnail"
android:layout_marginLeft="25dp"
android:text="Title"
/>
Expand Down

0 comments on commit d34a940

Please sign in to comment.