Skip to content

Instantly share code, notes, and snippets.

@RowlandOti
Created August 2, 2016 17:32
Show Gist options
  • Save RowlandOti/b2b197b3abc14962b5d5181dac858c12 to your computer and use it in GitHub Desktop.
Save RowlandOti/b2b197b3abc14962b5d5181dac858c12 to your computer and use it in GitHub Desktop.
package com.example.android.booklisting;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public static final String LOG_TAG = MainActivity.class.getSimpleName();
private static final String BOOK_SEARCH_API = "https://www.googleapis.com/books/v1/volumes?q=";
private static final String SEARCH_TERM = "term";
private String searchTerm;
private List<Result> result;
private ListView listView;
private ResultsAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = new ArrayList();
listView = (ListView) findViewById(R.id.list);
adapter = new ResultsAdapter(this);
listView.setAdapter(adapter);
if (savedInstanceState != null) {
}
Button submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SearchAsyncTask task = new SearchAsyncTask();
task.execute();
}
});
}
//Update the UI for the ListView with results
private void updateUi(List<Result> resultList) {
adapter.clear();
adapter.addAll(resultList);
adapter.notifyDataSetChanged();
}
/*
*Async task to connect and execute gathering search results from Google API
*/
class SearchAsyncTask extends AsyncTask<URL, Void, List<Result>> {
TextView search = (TextView) findViewById(R.id.search);
String query = search.getText().toString();
@Override
protected List<Result> doInBackground(URL... urls) {
String[] searchQuery;
if (query.contains(" ")) {
searchQuery = query.split(" ");
query = searchQuery[0] + searchQuery[1];
}
// Create URL object
URL url = createUrl(BOOK_SEARCH_API + query + "&maxResults=10");
// Perform HTTP request to the URL and receive a JSON response back
String jsonResponse = "";
try {
jsonResponse = makeHttpRequest(url);
Log.d("LOOKER", "We got some data" + jsonResponse);
} catch (IOException e) {
Log.e(LOG_TAG, "Error with the the url/search.");
}
//Place extracted json response in -Result object
result = extractItemFromJson(jsonResponse);
Log.d(MainActivity.class.getSimpleName(), "We got some data" + result.toArray().toString());
return result;
}
/**
* Set empty TextView just in case there is no result
*
* @param result two outputs(title, author) from custom class
*/
@Override
protected void onPostExecute(List<Result> result) {
TextView emptyText = (TextView) findViewById(R.id.empty_text_view);
if (result == null) {
emptyText.setText("Cannot be found.");
} else {
updateUi(result);
}
}
private boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
return isConnected;
}
/**
* Create the url being sent to the internet
*
* @param stringUrl the url being sent
* @return either null if error or the proper url
*/
private URL createUrl(String stringUrl) {
URL url = null;
try {
url = new URL(stringUrl);
} catch (MalformedURLException exception) {
Log.e(LOG_TAG, "Error with creating URL", exception);
return null;
}
return url;
}
/**
* Requesting the HTTP in the web browser
*
* @param url the url being sent in teh request
* @return the jsonresponse or null depending on the url
* @throws IOException propability of the url possibly being invalid
*/
private String makeHttpRequest(URL url) throws IOException {
String jsonResponse = "";
if (url == null) {
return jsonResponse;
}
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(10000 /* milliseconds */);
urlConnection.setConnectTimeout(15000 /* milliseconds */);
urlConnection.connect();
if (urlConnection.getResponseCode() == 200) {
inputStream = urlConnection.getInputStream();
jsonResponse = readFromStream(inputStream);
} else {
Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode());
}
} catch (IOException e) {
// Handle the exception
Log.e(LOG_TAG, "The input/output is not properly working", e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputStream != null) {
// function must handle IOException here
inputStream.close();
}
}
return jsonResponse;
}
/**
* Read the input and piecing it together into a string for jsonResponse
*
* @param inputStream information from the site after connection
* @return null or the string depending on inputStream
* @throws IOException whether the input is in valid range
*/
private String readFromStream(InputStream inputStream) throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputStreamReader);
String line = reader.readLine();
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
return output.toString();
}
/**
* Extracting relevant information needed from the json
*
* @param resultsJSON returned from the Google API site
* @return null or new Result(title, author) depending on resultsJSON
*/
private List<Result> extractItemFromJson(String resultsJSON) {
List<Result> searchResult = new ArrayList();
try {
JSONObject baseJsonResponse = new JSONObject(resultsJSON);
JSONArray itemArray = baseJsonResponse.getJSONArray("items");
if (itemArray.length() > 0) {
for (int i = 0; i < itemArray.length(); i++) {
JSONObject responseObject = itemArray.getJSONObject(i);
JSONObject volumeInfo = responseObject.getJSONObject("volumeInfo");
JSONArray authors = volumeInfo.getJSONArray("authors");
String title = volumeInfo.getString("title");
String author = authors.getString(0);
searchResult.add(new Result(title, author));
Log.d("LOOKER", "We got some data" + title);
}
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Problem parsing the search result JSON.");
}
Log.d("LOOKER", "We got some data" + searchResult.toArray().toString());
return searchResult;
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putString(SEARCH_TERM, searchTerm);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
searchTerm = savedInstanceState.getString(SEARCH_TERM);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment