Running Async in a fragment is not even being ran

69 views Asked by At

I am running the commands like so:

new GetGameScoresFromFuhantikAPI()

And my method for this is ->

    private class GetGameScoresFromFuhantikAPI extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            MethodContants.showLog(TAG, "Loading FUHNATIK API", true);

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            HttpHandler sh = new HttpHandler();
            // Making a request to url and getting response


            String url = API_URL + jsonFile;
//                String url = "http://www.nfl.com/liveupdate/game-center/" + list.get(i) + "/" + list.get(i) + "_gtd.json";
            String jsonStr = sh.makeServiceCall(url);

            Log.e(TAG, "Response from FUHNATIK API: " + url);
            if (jsonStr != null) {
                try {

                    //JSONObject object = new JSONObject(json);
                    JSONObject object = new JSONObject(jsonStr);

                    currentWeek = object.getString("pypwk");
                    currentWeekDB = object.getString("mdb");
                    JSONArray array = (JSONArray) object.get("g");

                    scheduleModelList = new ArrayList<>();

                    for (int i = 0; i < array.length(); i++) {
                        //TODO IF WE DONT PLAY THURSDAY GAMES PUT LIST.ADD IN HERE

//                if (!array.getJSONObject(i).getString("-d").equals("Thu")){
//
//                }

                        scheduleModelList.add(new ScheduleModel(array.getJSONObject(i).getString("-v"),
                                array.getJSONObject(i).getString("-h"),
                                array.getJSONObject(i).getString("-t"),
                                array.getJSONObject(i).getString("-d"),
                                array.getJSONObject(i).getString("-eid").substring(0, 8),
                                array.getJSONObject(i).getString("-t") + array.getJSONObject(i).getString("-q"),
                                array.getJSONObject(i).getString("-vnn"),
                                array.getJSONObject(i).getString("-hnn"),
                                "...select a team...",
                                array.getJSONObject(i).getString("-eid"),
                                array.getJSONObject(i).getString("-vs"),
                                array.getJSONObject(i).getString("-hs"),
                                array.getJSONObject(i).getString("-w")));



                    }


                } catch (final JSONException e) {
                    Log.e(TAG, "FUHNATIK API: Json parsing error: " + e.getMessage());
                }
            } else {



                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.e(TAG, "FUHNATIK API: Couldn't get json from server.");
                        Toast.makeText(getContext(), "Getting from ESPN", Toast.LENGTH_SHORT).show();
                        //new GetGameScoresFromESPN().execute();
                    }
                });
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            final ListViewAdapterResults adapter = new ListViewAdapterResults(listView.getContext(), scheduleModelList);
            listView.setAdapter(adapter);
            MethodContants.showLog(TAG, "DONE WITH LOADING FUHNATIK API", false);
        }
    }

I can't seem to figure out why the code never get ran. I ran through the debugger but I really can't pinpoint where this is failing out. Any help on this would be appreciated.

Eventually if the json file is not at this URL, I will be getting the json from NFL. However, Without this working, the ESPN won't work either, and I really can not figure out where the error is on my end. I have to assume this will be a pretty easy fix.

Again, as said before, any help would be very appreciated!!

1

There are 1 answers

0
AudioBubble On BEST ANSWER

In the above line of code you call the Class but you forgot to execute your asynctask. So no Overriden methods are called. Try this:

new GetGameScoresFromFuhantikAPI().execute();

If you want to pass something as argument, give parameters separated by coma like this:

new GetGameScoresFromFuhantikAPI().execute(arg0, arg1);