https://github.com/udacity/ud851-Exercises/compare/T05b.02-Exercise-AddAsyncTaskLoader…T05b.02-Solution-AddAsyncTaskLoader
@@ -15,9 +15,12 @@
*/
package com.example.android.asynctaskloader;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@@ -30,17 +33,21 @@
import java.io.IOException;
import java.net.URL;
// TODO (1) implement LoaderManager.LoaderCallbacks<String> on MainActivity
public class MainActivity extends AppCompatActivity {
// COMPLETED (1) implement LoaderManager.LoaderCallbacks<String> on MainActivity
public class MainActivity extends AppCompatActivity implements
LoaderManager.LoaderCallbacks<String> {
/* A constant to save and restore the URL that is being displayed */
private static final String SEARCH_QUERY_URL_EXTRA = "query";
// TODO (28) Remove the key for storing the search results JSON
/* A constant to save and restore the JSON that is being displayed */
private static final String SEARCH_RESULTS_RAW_JSON = "results";
// COMPLETED (28) Remove the key for storing the search results JSON
// TODO (2) Create a constant int to uniquely identify your loader. Call it GITHUB_SEARCH_LOADER
// COMPLETED (2) Create a constant int to uniquely identify your loader. Call it GITHUB_SEARCH_LOADER
/*
* This number will uniquely identify our Loader and is chosen arbitrarily. You can change this
* to any number you like, as long as you use the same variable name.
*/
private static final int GITHUB_SEARCH_LOADER = 22;
private EditText mSearchBoxEditText;
@@ -67,16 +74,17 @@ protected void onCreate(Bundle savedInstanceState) {
if (savedInstanceState != null) {
String queryUrl = savedInstanceState.getString(SEARCH_QUERY_URL_EXTRA);
// TODO (26) Remove the code that retrieves the JSON
String rawJsonSearchResults = savedInstanceState.getString(SEARCH_RESULTS_RAW_JSON);
// COMPLETED (26) Remove the code that retrieves the JSON
mUrlDisplayTextView.setText(queryUrl);
// TODO (25) Remove the code that displays the JSON
mSearchResultsTextView.setText(rawJsonSearchResults);
// COMPLETED (25) Remove the code that displays the JSON
}
// TODO (24) Initialize the loader with GITHUB_SEARCH_LOADER as the ID, null for the bundle, and this for the callback
// COMPLETED (24) Initialize the loader with GITHUB_SEARCH_LOADER as the ID, null for the bundle, and this for the callback
/*
* Initialize the loader
*/
getSupportLoaderManager().initLoader(GITHUB_SEARCH_LOADER, null, this);
}
/**
@@ -87,20 +95,57 @@ protected void onCreate(Bundle savedInstanceState) {
private void makeGithubSearchQuery() {
String githubQuery = mSearchBoxEditText.getText().toString();
// TODO (17) If no search was entered, indicate that there isn't anything to search for and return
// COMPLETED (17) If no search was entered, indicate that there isn't anything to search for and return
/*
* If the user didn't enter anything, there's nothing to search for. In the case where no
* search text was entered but the search button was clicked, we will display a message
* stating that there is nothing to search for and we will not attempt to load anything.
*
* If there is text entered in the search box when the search button was clicked, we will
* create the URL that will return our Github search results, display that URL, and then
* pass that URL to the Loader. The reason we pass the URL as a String is simply a matter
* of convenience. There are other ways of achieving this same result, but we felt this
* was the simplest.
*/
if (TextUtils.isEmpty(githubQuery)) {
mUrlDisplayTextView.setText("No query entered, nothing to search for.");
return;
}
URL githubSearchUrl = NetworkUtils.buildUrl(githubQuery);
mUrlDisplayTextView.setText(githubSearchUrl.toString());
// TODO (18) Remove the call to execute the AsyncTask
new GithubQueryTask().execute(githubSearchUrl);
// TODO (19) Create a bundle called queryBundle
// TODO (20) Use putString with SEARCH_QUERY_URL_EXTRA as the key and the String value of the URL as the value
// TODO (21) Call getSupportLoaderManager and store it in a LoaderManager variable
// TODO (22) Get our Loader by calling getLoader and passing the ID we specified
// TODO (23) If the Loader was null, initialize it. Else, restart it.
// COMPLETED (18) Remove the call to execute the AsyncTask
// COMPLETED (19) Create a bundle called queryBundle
Bundle queryBundle = new Bundle();
// COMPLETED (20) Use putString with SEARCH_QUERY_URL_EXTRA as the key and the String value of the URL as the value
queryBundle.putString(SEARCH_QUERY_URL_EXTRA, githubSearchUrl.toString());
/*
* Now that we've created our bundle that we will pass to our Loader, we need to decide
* if we should restart the loader (if the loader already existed) or if we need to
* initialize the loader (if the loader did NOT already exist).
*
* We do this by first store the support loader manager in the variable loaderManager.
* All things related to the Loader go through through the LoaderManager. Once we have a
* hold on the support loader manager, (loaderManager) we can attempt to access our
* githubSearchLoader. To do this, we use LoaderManager's method, "getLoader", and pass in
* the ID we assigned in its creation. You can think of this process similar to finding a
* View by ID. We give the LoaderManager an ID and it returns a loader (if one exists). If
* one doesn't exist, we tell the LoaderManager to create one. If one does exist, we tell
* the LoaderManager to restart it.
*/
// COMPLETED (21) Call getSupportLoaderManager and store it in a LoaderManager variable
LoaderManager loaderManager = getSupportLoaderManager();
// COMPLETED (22) Get our Loader by calling getLoader and passing the ID we specified
Loader<String> githubSearchLoader = loaderManager.getLoader(GITHUB_SEARCH_LOADER);
// COMPLETED (23) If the Loader was null, initialize it. Else, restart it.
if (githubSearchLoader == null) {
loaderManager.initLoader(GITHUB_SEARCH_LOADER, queryBundle, this);
} else {
loaderManager.restartLoader(GITHUB_SEARCH_LOADER, queryBundle, this);
}
}
/**
@@ -130,73 +175,93 @@ private void showErrorMessage() {
/* Then, show the error */
mErrorMessageDisplay.setVisibility(View.VISIBLE);
}
// TODO (3) Override onCreateLoader
// Within onCreateLoader
// TODO (4) Return a new AsyncTaskLoader<String> as an anonymous inner class with this as the constructor's parameter
// TODO (5) Override onStartLoading
// Within onStartLoading
// TODO (6) If args is null, return.
// TODO (7) Show the loading indicator
// TODO (8) Force a load
// END - onStartLoading
// TODO (9) Override loadInBackground
// Within loadInBackground
// TODO (10) Get the String for our URL from the bundle passed to onCreateLoader
// TODO (11) If the URL is null or empty, return null
// TODO (12) Copy the try / catch block from the AsyncTask's doInBackground method
// END - loadInBackground
// TODO (13) Override onLoadFinished
// Within onLoadFinished
// TODO (14) Hide the loading indicator
// TODO (15) Use the same logic used in onPostExecute to show the data or the error message
// END - onLoadFinished
// TODO (16) Override onLoaderReset as it is part of the interface we implement, but don't do anything in this method
// TODO (29) Delete the AsyncTask class
public class GithubQueryTask extends AsyncTask<URL, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mLoadingIndicator.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(URL... params) {
URL searchUrl = params[0];
String githubSearchResults = null;
try {
githubSearchResults = NetworkUtils.getResponseFromHttpUrl(searchUrl);
} catch (IOException e) {
e.printStackTrace();
// COMPLETED (3) Override onCreateLoader
@Override
public Loader<String> onCreateLoader(int id, final Bundle args) {
// COMPLETED (4) Return a new AsyncTaskLoader<String> as an anonymous inner class with this as the constructor's parameter
return new AsyncTaskLoader<String>(this) {
// COMPLETED (5) Override onStartLoading
@Override
protected void onStartLoading() {
// COMPLETED (6) If args is null, return.
/* If no arguments were passed, we don't have a query to perform. Simply return. */
if (args == null) {
return;
}
// COMPLETED (7) Show the loading indicator
/*
* When we initially begin loading in the background, we want to display the
* loading indicator to the user
*/
mLoadingIndicator.setVisibility(View.VISIBLE);
// COMPLETED (8) Force a load
forceLoad();
}
return githubSearchResults;
}
@Override
protected void onPostExecute(String githubSearchResults) {
mLoadingIndicator.setVisibility(View.INVISIBLE);
if (githubSearchResults != null && !githubSearchResults.equals("")) {
showJsonDataView();
mSearchResultsTextView.setText(githubSearchResults);
} else {
showErrorMessage();
// COMPLETED (9) Override loadInBackground
@Override
public String loadInBackground() {
// COMPLETED (10) Get the String for our URL from the bundle passed to onCreateLoader
/* Extract the search query from the args using our constant */
String searchQueryUrlString = args.getString(SEARCH_QUERY_URL_EXTRA);
// COMPLETED (11) If the URL is null or empty, return null
/* If the user didn't enter anything, there's nothing to search for */
if (TextUtils.isEmpty(searchQueryUrlString)) {
return null;
}
// COMPLETED (12) Copy the try / catch block from the AsyncTask's doInBackground method
/* Parse the URL from the passed in String and perform the search */
try {
URL githubUrl = new URL(searchQueryUrlString);
String githubSearchResults = NetworkUtils.getResponseFromHttpUrl(githubUrl);
return githubSearchResults;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
};
}
// COMPLETED (13) Override onLoadFinished
@Override
public void onLoadFinished(Loader<String> loader, String data) {
// COMPLETED (14) Hide the loading indicator
/* When we finish loading, we want to hide the loading indicator from the user. */
mLoadingIndicator.setVisibility(View.INVISIBLE);
// COMPLETED (15) Use the same logic used in onPostExecute to show the data or the error message
/*
* If the results are null, we assume an error has occurred. There are much more robust
* methods for checking errors, but we wanted to keep this particular example simple.
*/
if (null == data) {
showErrorMessage();
} else {
mSearchResultsTextView.setText(data);
showJsonDataView();
}
}
// COMPLETED (16) Override onLoaderReset as it is part of the interface we implement, but don't do anything in this method
@Override
public void onLoaderReset(Loader<String> loader) {
/*
* We aren't using this method in our example application, but we are required to Override
* it to implement the LoaderCallbacks<String> interface
*/
}
// COMPLETED (29) Delete the AsyncTask class
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
@@ -220,8 +285,6 @@ protected void onSaveInstanceState(Bundle outState) {
String queryUrl = mUrlDisplayTextView.getText().toString();
outState.putString(SEARCH_QUERY_URL_EXTRA, queryUrl);
// TODO (27) Remove the code that persists the JSON
String rawJsonSearchResults = mSearchResultsTextView.getText().toString();
outState.putString(SEARCH_RESULTS_RAW_JSON, rawJsonSearchResults);
// COMPLETED (27) Remove the code that persists the JSON
}
}
https://github.com/udacity/ud851-Exercises/blob/daff407378fd9557851f70716c4c238876e742cc/app/src/main/java/com/example/android/asynctaskloader/utilities/NetworkUtils.java