Android Async Tsk没有正确执行

时间:2018-03-28 17:27:05

标签: java android android-asynctask

我只是想在启动屏幕中等待,直到我的JSON正在处理,但事实并非如此。 onPostExecute()方法在 doinbackground()之后执行。没有时间加载整个JSON。

这是我的代码。我首先获得列表大小为零,然后JSON将开始加载。

private class GetContacts extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected Void doInBackground(Void... arg0) {
        datalist = new ArrayList<>();
        Log.e("entering","Enter Inside");
        try{
            JsonArrayRequest billionaireReq =
                    new JsonArrayRequest(new ApiURLs().getAll_movies_url() 
                            new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {
                            Log.e("response", "Response" + response);
                            //Initialize Gson obj to process jason response
                            GsonBuilder gsonBuilder = new GsonBuilder();
                            Gson gson = gsonBuilder.create();
                            // assigning data in model class, we initializing this class as a array type
                            // because the response is in array format
                            HomeModel[] getResult = gson.fromJson(response.toString(), HomeModel[].class);

                            // this loop help us in fetching all the records of model class
                            for (int i = 0; i < getResult.length; i++) {
                                Log.e("response", "Model Values" + getResult[i].toString());
                                //datalist.add(getResult[i]);
                                datalist.add(getResult[i]);
                            }

                            //GlobalData.setGlobal_movie_list(datalist);
                            Log.e("DataLoded","Record Added");
                        }

                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.e("Request","Error" + error);
                        }
                    });

            //This is a queue which is used by Volley to lineup requests
            AppController.getInstance().addToRequestQueue(billionaireReq);
        }
        catch (Exception e){
            Log.e("Error","Error"+e);
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        Log.e("Data","DataSize"+datalist.size());

    }
} 

0 个答案:

没有答案