Created
March 17, 2018 17:26
-
-
Save eltonjhony/00cc84e67255ff686f2185f8af5d7c02 to your computer and use it in GitHub Desktop.
AsyncTask that provide an listener to handle callbacks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.net.Uri; | |
import android.os.AsyncTask; | |
import android.util.Log; | |
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.URL; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by eltonjhony on 17/03/18. | |
*/ | |
public class FetchMovieListTask extends AsyncTask<String, Void, List<Info>> { | |
private final String LOG_TAG = FetchMovieListTask.class.getSimpleName(); | |
private static final String API_KEY = "Put your KEY here"; | |
private static final String BASE_IMG_PATH = "http://image.tmdb.org/t/p/w500"; | |
private final String BASE_URL = "http://api.themoviedb.org/3/discover/movie?api_key=" + API_KEY; | |
private OnLoadMoviesListener listener; | |
public FetchMovieListTask(OnLoadMoviesListener listener) { | |
this.listener = listener; | |
} | |
@Override | |
protected List<Info> doInBackground(String... params) { | |
Log.d(LOG_TAG, "Execute task"); | |
if (params == null || params.length == 0) { | |
return null; | |
} | |
HttpURLConnection urlConnection = null; | |
BufferedReader reader = null; | |
List<Info> movieList; | |
try { | |
Uri.Builder builder = Uri.parse(BASE_URL).buildUpon(); | |
builder.appendQueryParameter("sort_by", params[0]); | |
URL url = new URL(builder.toString()); | |
Log.d(LOG_TAG, "URL: " + url); | |
// Create the request to OpenWeatherMap, and open the connection | |
urlConnection = (HttpURLConnection) url.openConnection(); | |
urlConnection.setRequestMethod("GET"); | |
urlConnection.connect(); | |
// Read the input stream into a String | |
InputStream inputStream = null; | |
try { | |
inputStream = urlConnection.getInputStream(); | |
} catch (IOException e) { | |
Log.e(LOG_TAG, e.getMessage()); | |
} | |
StringBuffer buffer = new StringBuffer(); | |
if (inputStream == null) { | |
// Nothing to do. | |
return null; | |
} | |
reader = new BufferedReader(new InputStreamReader(inputStream)); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing) | |
// But it does make debugging a *lot* easier if you print out the completed | |
// buffer for debugging. | |
buffer.append(line + "\n"); | |
} | |
if (buffer.length() == 0) { | |
// Stream was empty. No point in parsing. | |
return null; | |
} | |
movieList = getMovieInfo(buffer.toString()); | |
} catch (IOException e) { | |
Log.e(LOG_TAG, "Error ", e); | |
return null; | |
} catch (JSONException e) { | |
Log.e(LOG_TAG, "Error ", e); | |
// If the code didn't successfully get the weather data, there's no point in attemping | |
// to parse it. | |
return null; | |
} finally { | |
if (urlConnection != null) { | |
urlConnection.disconnect(); | |
} | |
if (reader != null) { | |
try { | |
reader.close(); | |
} catch (final IOException e) { | |
Log.e(LOG_TAG, "Error closing stream", e); | |
} | |
} | |
} | |
Log.d(LOG_TAG, "Execute task finish"); | |
return movieList; | |
} | |
@Override | |
protected void onPostExecute(List<Info> infos) { | |
super.onPostExecute(infos); | |
listener.onSuccess(infos); | |
} | |
private List<Info> getMovieInfo(String jsonStr) throws JSONException { | |
JSONObject jsonObject = new JSONObject(jsonStr); | |
JSONArray movieListJson = jsonObject.getJSONArray("results"); | |
List<Info> pathList = new ArrayList<Info>(); | |
for (int i = 0; i < movieListJson.length(); i++) { | |
JSONObject movieJson = movieListJson.getJSONObject(i); | |
Long id = movieJson.getLong("id"); | |
String posterPath = BASE_IMG_PATH + movieJson.getString("poster_path"); | |
String overview = movieJson.getString("overview"); | |
String release_date = movieJson.getString("release_date"); | |
String original_title = movieJson.getString("original_title"); | |
Double vote_average = movieJson.getDouble("vote_average"); | |
Info movieInfo = new Info(id, posterPath, overview, release_date, original_title, vote_average); | |
pathList.add(movieInfo); | |
} | |
return pathList; | |
} | |
interface OnLoadMoviesListener { | |
void onSuccess(List<Info> result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment