How to make transverse data in a handler thread

I have an activity that contains 2 fragments, each fragment has 2 list views, each has an adapter. Therefore, I get fairly long data jsonfrom the server, I use AsyncTaskand post execute, I analyze the data. The problem is that it freezes the loading animation for 4-5 seconds.

I tried using a stream Handler, but the problem still exists, I have to do something terrible here.

public class DataService extends AsyncTask<String, String, Void> {

    @Override
        protected Void doInBackground(String... params) {

        url = new URL(DATA_URL);    
        //Connection stuff

        String jsonString = stringBuilder.toString();
        jsonObject = new JSONObject(jsonString);

        //Adding data in Java Objects

    }


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


             //Upcoming List in Fragment
             allDataFragment.setUpcomingDataList(UpcomingAllDataList);
             awayFragment.setUpcomingDataList(tUpcomingAwayList);
             homeFragment.setUpcomingDataList(tUpcomingHomeList);


             //Past List in Fragment            
             allDataFragment.setPastDataList(pastAllDataList);
             awayFragment.setPastDataList(pastAwayList);
             homeFragment.setPastDataList(pastHomeList);    

    }

I added log messages to the adapter, and I see that it hangs during parsing, so it accepts all messages executed in the handler thread

       Handler handler= new Handler();

        handler.post(new Runnable() {
            @Override
            public void run() {


                    //Upcoming List in Fragment
                     allDataFragment.setUpcomingDataList(UpcomingAllDataList);
                     awayFragment.setUpcomingDataList(tUpcomingAwayList);
                     homeFragment.setUpcomingDataList(tUpcomingHomeList);


                     //Past List in Fragment            
                     allDataFragment.setPastDataList(pastAllDataList);
                     awayFragment.setPastDataList(pastAwayList);
                     homeFragment.setPastDataList(pastHomeList);    


            }                   
            });

I also tried adding a handler to Adapter, but it could not load the component UI.

This is the code snippet of my snippet.

if (pastListView != null) {
        PastListAdapter allGamesAdapter = new PastListAdapter(getContext(), pastAllDataList);
        pastListView.setAdapter(allGamesAdapter);
        }

Adapter .

  @Override
    public View getView(int position, View convertView, ViewGroup parent) {

            TextView vScore = (TextView) v.findViewById(R.id.v_score);
            TextView date = (TextView) v.findViewById(R.id.date);
            ImageView image = (ImageView) v.findViewById(R.id.iv_image);
            //7 Other views


            vScore.setText(dataList.get(position).getScore);
            date.setText(dataList.get(position).date);

             Log.d(TAG, "getView: Row");

            return v;
    }

, , , .

+4
5

AsyncTask 5 (5 ). JSON , (Thread, HandlerThread ThreadPoolExecutor)

Handler, . Handler HandlerThread

:

  • HandlerThread JSON . .

  • HandlerThread Handler . , handleMessage(Message msg)

:

Asynctask vs Thread vs Services vs Loader

AsyncTask vs Thread

, runOnUiThread ? ( )

+3

Volley AsyncTask

+2

ui, ui

asyncTask, json

void

, onPostExecute doInBackground

+1

, , ,

Retrofit - RESTfull

OKhttp - http

AsyncTasks , .

, , . AsyncTask .

AsyncTask . , Params, Progress Result, 4 , begin, doInBackground, . , , 4 :

1 . onPreExecute(), . , , , .

2 . doInBackground (Params...), , onPreExecute() . , . . . publishProgress (Progress...) . onProgressUpdate (Progress...).

3 . onProgressUpdate (Progress...), publishProgress (Progress...). undefined. , . , .

4 . onPostExecute (), . .

, , , 2 .

public class DataService extends AsyncTask<String, String, List<UpcomingAllDataList>> {

    @Override
    protected List<UpcomingAllDataList> doInBackground(String... params) {

        // making HTTP request

        return UpcomingAllDataList; // return results of Http request
    }

    @Override
    protected void onPostExecute(List<UpcomingAllDataList> upcomingAllDataList) {
        super.onPostExecute(upcomingAllDataList);


        //Upcoming List in Fragment
        allDataFragment.setUpcomingDataList(upcomingAllDataList);

        // all another work

    }
}

, .

+1

Retrofit + parsing.
Github

, , setUpcomingDataList ( ).

, HandlerThread , Handler , Handler on ( ) Runnable .

anyway 2 - , ( , android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.), :

Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    //Upcoming List in Fragment
                    allDataFragment.setUpcomingDataList(UpcomingAllDataList);
                    awayFragment.setUpcomingDataList(tUpcomingAwayList);
                    homeFragment.setUpcomingDataList(tUpcomingHomeList);


                    //Past List in Fragment            
                    allDataFragment.setPastDataList(pastAllDataList);
                    awayFragment.setPastDataList(pastAwayList);
                    homeFragment.setPastDataList(pastHomeList);
                }
            });
            thread.start();

which is really bad practice - as I said earlier, use Retrofit.

0
source

Source: https://habr.com/ru/post/1687284/


All Articles