Ian Bell
Ian Bell

Reputation: 543

How to instantiate a new JSONObject Arraylist which takes the value of the Arraylist returned by a function?

My defined function returns a JSONObejct Arraylist, however, when I instantiate a new Arraylist to the output of the function, it shows an empty Arraylist. How can I fix this issue and why is it showing an empty array list when it is indeed returning an Arraylist in the function?

Here is the code :


protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activty_departures);

      departure_flights = doGetRequest();
}

//my function

private ArrayList<JSONObject> doGetRequest() {
        OkHttpClient client = new OkHttpClient();
        ArrayList<JSONObject> departureObject = new ArrayList<>();
        String url = "http_url";

        Request request = new Request.Builder().url(url).build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                if(response.isSuccessful()) {

                    try {
                        String jsonData = response.body().string();
                        JSONObject Jobject = new JSONObject(jsonData);
                        JSONArray jarray = Jobject.getJSONArray("Flights");

                        for (int i = 0; i < jarray.length(); i++) {
                            JSONObject object = jarray.getJSONObject(i);
                            String adft = object.getString("Adft");
                            if (adft.equals("D")) {
                                departureObject.add(object);
                            }
                        }
                        

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

            }
        });

        return departureObject;

Upvotes: 0

Views: 62

Answers (3)

Muhammad Asad
Muhammad Asad

Reputation: 668

Hitting Api in android not getting immediately return data it depends upon your response. you are to return the list immediately so you received an empty list if you can work inside the onResponse method then your problem is solved.

Upvotes: 1

M.Muzammil
M.Muzammil

Reputation: 683

You're returning the list immediately after enqueue your API. Your ArrayList fill after API request succeeds so you have to create your ArrayList global and fill that after onSuccess. After that create another method to render your data on UI. like mentioned below:

ArrayList<JSONObject> departureObject = new ArrayList<>();

protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activty_departures);

      doGetRequest();
}

private void doGetRequest() {

    OkHttpClient client = new OkHttpClient();
    String url = "http_url";

    Request request = new Request.Builder().url(url).build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {

            if(response.isSuccessful()) {

                try {
                    String jsonData = response.body().string();
                    JSONObject Jobject = new JSONObject(jsonData);
                    JSONArray jarray = Jobject.getJSONArray("Flights");

                    for (int i = 0; i < jarray.length(); i++) {
                        JSONObject object = jarray.getJSONObject(i);
                        String adft = object.getString("Adft");
                        if (adft.equals("D")) {
                            departureObject.add(object);
                        }
                    }
                    
                        reloadData();

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

        }
    }); 
}

private void reloadData() {
    // here your data is ready
}

Upvotes: 0

SlothCoding
SlothCoding

Reputation: 1706

Don't use .equals() method on String, but use .contentEquals(). The reason for this is because contentEquals() checks the content of a String and compares it to StringBuffer, StringBuilder and CharSequence aswell and all derived classes of these.

This is why in your case adft.equals("D") could return false even though adft is in the background this:

String adft = "D";

The reason for that is because equals() will only compare String objects, so all other objects are considered not equal and it will return false.

More on that here: https://www.programmersought.com/article/2993983603/

Also, sometimes returned values can store a space we dont need, so insted "D" we have "D " or " D". To solve this just use method .trim()

if(adft.trim().contentEquals("D"))

Upvotes: 1

Related Questions