0

I am using ArrayList in onPostExecute(AsyncTask) on Fragment.

The problem is that arrivalInfoArrayList is empty after I executed AsyncTask.

I tried to use

1)

getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
        //Code for the UiThread
    }
});

2)

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

            }
        });

But it did not work. How can I solve this? This is my code.

    arrivalAsync = new ArrivalAsync() {
        @Override
        protected void onPostExecute(String arrivalUrl) {
            try {
                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                factory.setNamespaceAware(true);
                XmlPullParser xpp = factory.newPullParser();

                xpp.setInput(new StringReader(arrivalUrl));
                int eventType = xpp.getEventType();
                while(eventType != XmlPullParser.END_DOCUMENT) {
                    if(eventType == XmlPullParser.START_DOCUMENT) {

                    } else if(eventType == XmlPullParser.START_TAG) {
                        String tagName = xpp.getName();

                        switch (tagName) {
                            case "arsId":
                                bl_arsId = true;
                                break;
                            case "firstTm":
                                bl_firstTm = true;
                                break;
                            case "lastTm":
                                bl_lastTm = true;
                            case "stNm":
                                bl_stNm = true;
                                break;
                        }
                    } else if(eventType == XmlPullParser.TEXT) {

                        if(bl_arsId) {
                            arsId = xpp.getText();
                            arrivalInfo.setArsId(arsId);
                            bl_arsId = false;
                        }

                        if(bl_firstTm) {
                            firstTm = xpp.getText();
                            arrivalInfo.setFirstTm(firstTm);
                            bl_firstTm = false;
                        }

                        if(bl_lastTm) {
                            lastTm = xpp.getText();
                            arrivalInfo.setLastTm(lastTm);
                            bl_lastTm = false;
                        }

                        if(bl_stNm) {
                            stNm = xpp.getText();
                            arrivalInfo.setStNm(stNm);
                            bl_stNm = false;
                        }


                    } else if(eventType == XmlPullParser.END_TAG) {
                        String tagName = xpp.getName();

                        if(tagName.equals("itemList"))  {

                            arrivalInfoArrayList.add(arrivalInfo);

                            arrivalInfo = new ArrivalInfo();
                        }
                    }
                    eventType = xpp.next();

                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    };

arrivalAsync.execute(arrivalUrl);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • You're not actually fetching the "url" but instead using a StringReader on the URL string which is not what you want. –  Aug 27 '19 at 16:37

2 Answers2

0

First thing first ,you should do such while loop computation in doInBackround() . Also did you try to print array list size in logs or tried debugging the execution to check if there is any data get added in your array list? If not then you should have done this basic home work.

Ashok Kumar
  • 1,226
  • 1
  • 10
  • 14
0

Assuming arrivalUrl is an actual URL string and the intent is to fetch/parse the URL response then you'll need to get an input stream to the URL endpoint as in below. But you must do this in background - doInBackground.

InputStream is = new URL(arrivalUrl).openConnection().getInputStream();
xpp.setInput(is, null);

and close the stream at the end

is.close();

and wrap everything with a try-block for possible exceptions.

Have the doInBackground return the arraylist and process it in onPostExecute.

Here's an answer which demonstrates this - you're not too far from it - basically turn your onPostExecute into doInBackground and return the ArrayList: https://stackoverflow.com/a/6343299/2711811