I'm trying to use AsyncTaskLoader to retrieve data then parse the resulting json file, however, I can't figure out why I keep getting a
return null;
instead of returning
return gameList;
DailyScheduleFragment.java
Method calling the Loader:
public void loadGameScheduleList() {
getLoaderManager().initLoader(LOADER_ID_DAILY_SCHEDULE, null, new LoaderCallbacks<ArrayList<Game>>() {
public Loader<ArrayList<Game>> onCreateLoader(int id, Bundle args) {
return new DailyScheduleLoader(DailyScheduleFragment.this.getContext());
}
public void onLoadFinished(Loader<ArrayList<Game>> loader, ArrayList<Game> data) {
if (data == null) {
Exception exception = ((DailyScheduleLoader) loader).getException();
if (exception == null) {
return;
}
if (exception instanceof NoNetworkException) {
DailyScheduleFragment.this._btnMessage.setText(R.string.general_msg_no_network_connection_turn_on);
DailyScheduleFragment.this._btnMessage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intents.networkSettings(DailyScheduleFragment.this.getContext());
}
});
DailyScheduleFragment.this._progressBar.setVisibility(View.GONE);
DailyScheduleFragment.this._btnMessage.setVisibility(View.GONE);
return;
}
DailyScheduleFragment.this._btnMessage.setText(R.string.general_msg_something_went_wrong);
DailyScheduleFragment.this._btnMessage.setOnClickListener(null);
DailyScheduleFragment.this._progressBar.setVisibility(View.GONE);
DailyScheduleFragment.this._btnMessage.setVisibility(View.GONE);
return;
}
DailyScheduleFragment.this._games.clear();
Iterator it = data.iterator();
while (it.hasNext()) {
DailyScheduleFragment.this._games.add((Game) it.next());
}
DailyScheduleFragment.this._adapter.notifyDataSetChanged();
DailyScheduleFragment.this.displayListState();
}
public void onLoaderReset(Loader<ArrayList<Game>> loader) {
}
}).forceLoad();
}
DailyScheduleLoader.java
public class DailyScheduleLoader extends AsyncTaskLoader<ArrayList<Game>> {
private Context _context;
private Exception _exception;
public DailyScheduleLoader(Context context) {
super(context);
this._context = context;
}
public ArrayList<Game> loadInBackground() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this._context);
try {
ArrayList<Game> gameList = Parse.gameList(Networking.sendHttpRequest("http://api.sportradar.us/nhl/trial/v5/en/games/2018/01/01/schedule.json?api_key=xxx", this._context));
return gameList;
} catch (Exception e) {
this._exception = e;
return null;
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}
public Exception getException() {
return this._exception;
}
Network class:
public static final class Networking {
public static String sendHttpRequest(String urlString, Context context) throws Throwable {
NoNetworkException e;
Exception ex;
Throwable th;
if (urlString == null || urlString.trim().equals(BuildConfig.FLAVOR)) {
throw new NullPointerException("urlString");
}
HttpURLConnection httpCon = null;
InputStream input_stream = null;
InputStreamReader input_stream_reader = null;
BufferedReader input = null;
StringBuilder response = new StringBuilder();
try {
if (isNetworkAvailable(context)) {
httpCon = (HttpURLConnection) new URL(urlString).openConnection();
if (httpCon.getResponseCode() != 200) {
Log.e("TAG", "Cannot Connect to : " + urlString);
if (input == null) {
return null;
}
try {
input_stream_reader.close();
input_stream.close();
input.close();
} catch (IOException e2) {
e2.printStackTrace();
}
if (httpCon == null) {
return null;
}
httpCon.disconnect();
return null;
}
input_stream = httpCon.getInputStream();
InputStreamReader input_stream_reader2 = new InputStreamReader(input_stream);
try {
BufferedReader input2 = new BufferedReader(input_stream_reader2);
while (true) {
try {
String line = input2.readLine();
if (line == null) {
break;
}
response.append(line).append("\n");
} catch (Exception e4) {
ex = e4;
input = input2;
input_stream_reader = input_stream_reader2;
} catch (Throwable th2) {
th = th2;
input = input2;
input_stream_reader = input_stream_reader2;
}
}
if (input2 != null) {
try {
input_stream_reader2.close();
input_stream.close();
input2.close();
} catch (IOException e22) {
e22.printStackTrace();
}
if (httpCon != null) {
httpCon.disconnect();
input = input2;
input_stream_reader = input_stream_reader2;
return response.toString();
}
}
input_stream_reader = input_stream_reader2;
} catch (Exception e6) {
ex = e6;
input_stream_reader = input_stream_reader2;
ex.printStackTrace();
if (input != null) {
try {
input_stream_reader.close();
input_stream.close();
input.close();
} catch (IOException e222) {
e222.printStackTrace();
}
if (httpCon != null) {
httpCon.disconnect();
}
}
return response.toString();
} catch (Throwable th4) {
th = th4;
input_stream_reader = input_stream_reader2;
if (input != null) {
try {
input_stream_reader.close();
input_stream.close();
input.close();
} catch (IOException e2222) {
e2222.printStackTrace();
}
if (httpCon != null) {
httpCon.disconnect();
}
}
throw th;
}
return response.toString();
}
throw new NoNetworkException();
} catch (NoNetworkException e7) {
e = e7;
throw e;
} catch (Exception e8) {
ex = e8;
ex.printStackTrace();
if (input != null) {
input_stream_reader.close();
input_stream.close();
input.close();
if (httpCon != null) {
httpCon.disconnect();
}
}
return response.toString();
}
}
public static boolean isNetworkAvailable(@NonNull Context context) {
@SuppressLint("WrongConstant") NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
}
Parse class:
public static class Parse {
public static ArrayList<Game> gameList(String json) throws JSONException {
ArrayList<Game> games = new ArrayList();
JSONArray results = new JSONObject(json).getJSONArray("games");
// looping through All Games
for (int i = 0; i < results.length(); i++) {
JSONObject gameJSON = results.getJSONObject(i);
Game game = new Game();
game.setId(gameJSON.getString(Responses.Games.VALUE_ID));
game.setStatus(gameJSON.getString(Responses.Games.VALUE_STATUS));
games.add(game);
}
return games;
}
}
I found the code on a website and trying to modify it to suit my needs. For the most part, it works well, except I can't figure out where I'm going wrong with this.
If it helps, here's the json file that's returned in the request (sorry about the formatting):
{"date":"2018-01-01","league":{"id":"fd560107-a85b-4388-ab0d-655ad022aff7","name":"NHL","alias":"NHL"},"games":[{"id":"6d20bdbd-b5e0-46ab-8b98-45ea01ab0e2b","status":"scheduled","coverage":"full","scheduled":"2018-01-01T18:00:00+00:00","reference":"20601","venue":{"id":"faebd24e-ccfb-4e7c-aa58-1dee9e82bb0f","name":"Citi Field","capacity":45000,"address":"123 Roosevelt Ave","city":"Queens","state":"NY","zip":"11368","country":"USA","time_zone":"US/Eastern"},"broadcast":{"network":"NBC"},"home":{"id":"4416d559-0f24-11e2-8525-18a905767e44","name":"Buffalo Sabres","alias":"BUF"},"away":{"id":"441781b9-0f24-11e2-8525-18a905767e44","name":"New York Rangers","alias":"NYR"}}]}
Go here if you want it formatted.
I've added break points and stepped through the code to check the variables. All seems right, up until the
return gameList;