运行 片段中的异步甚至不是 运行

Running Async in a fragment is not even being ran

我是运行这样的命令:

new GetGameScoresFromFuhantikAPI()

我的方法是 ->

    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);
        }
    }

我似乎无法弄清楚为什么代码永远不会 运行。我 运行 通过调试器,但我真的无法查明失败的地方。如有任何帮助,我们将不胜感激。

最终,如果 json 文件不在 URL,我将从 NFL 获得 json。然而,如果没有这个工作,ESPN 也将无法工作,我真的无法弄清楚我的错误在哪里。我必须假设这将是一个非常简单的修复。

同样,如前所述,我们将不胜感激!!

在上面的代码行中,您调用了 Class 但您忘记执行异步任务。所以没有调用覆盖方法。试试这个:

new GetGameScoresFromFuhantikAPI().execute();

如果你想传递一些东西作为参数,像这样给出用逗号分隔的参数:

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