简体   繁体   English

在片段中运行异步甚至没有被运行

[英]Running Async in a fragment is not even being ran

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. 最终,如果json文件不在此URL,我将从NFL获取json。 However, Without this working, the ESPN won't work either, and I really can not figure out where the error is on my end. 但是,如果没有这项工作,ESPN也将无法工作,而且我真的无法弄清楚错误到底在哪里。 I have to assume this will be a pretty easy fix. 我必须假设这将是一个非常简单的修复。

Again, as said before, any help would be very appreciated!! 再次,如前所述,任何帮助将不胜感激!

In the above line of code you call the Class but you forgot to execute your asynctask. 在上面的代码行中,您调用了Class,但是您忘记了执行asynctask。 So no Overriden methods are called. 因此,没有调用Overriden方法。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM