0

I try to add data from my object to ArrayList but it's not work.

This code read data from JSON and add to ArrayList in MySQLConnect.java like this.

    private ComputerService computerservice;
    public static ArrayList<ComputerService> computerServicesArrayList = new ArrayList<>();
    private String URL = "http://10.200.100.10/", GET_URL = "android/get_data.php";

    public MySQLConnect(){
        main = null;

    }

    public MySQLConnect(Activity mainA){
        main = mainA;
    }

    public List<ComputerService> getData(){
        String url = URL + GET_URL;
        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                showJSON(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(main, error.getMessage().toString(), LENGTH_LONG).show();
            }
        }

        );

        RequestQueue requestQueue = Volley.newRequestQueue(main.getApplicationContext());
        requestQueue.add(stringRequest);

        return computerServicesArrayList;
    }

    public void showJSON(String response){
        String data_mysql = "";
        computerServicesArrayList.clear();
        try{
            JSONObject jsonObject = new JSONObject(response);
            JSONArray result = jsonObject.getJSONArray("data");

            for(int i=0; i < result.length(); i++){
                JSONObject collectData = result.getJSONObject(i);
                String id = collectData.getString("id");
                String type = collectData.getString("type");
                String address = collectData.getString("address");

                computerservice = new ComputerService(id, type, address);
                computerServicesArrayList.add(computerservice);

            }

        System.out.println("Size in class MySQLConnect");
        System.out.println(computerServicesArrayList.size());

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

The MainActivity.java I show computerServicesArrayList.size() like this.

 public static List<ComputerService> computerServicesArrayList;

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

        mySQLConnect = new MySQLConnect(MainActivity.this);

        update();
    }


    public void update(){
        computerServicesArrayList =  mySQLConnect.getData();
        System.out.println("Size in MainActivity");
        System.out.println(computerServicesArrayList.size());
}

The output show like this.

Size in MainActivity
    0

Size in class MySQLConnect
83

From the code I can print computerServicesArrayList.size() the result is 83 but when I print from MainActivity why it show result 0. How to fix it?

user58519
  • 579
  • 1
  • 4
  • 19

2 Answers2

2

I don't know the Volley framework/classes in detail. But it looks like you are creating an asynchronous request. So your rest-request gets send and when the response comes in your showJSON() method is called.

But you immediatley return the computerServicesArrayList result, which is empty because you don't have your response yet. This is also the reason why the print statement from your MainActivity is executed before the print from your showJSON method.

If you want to wait for the rest-response you have to do synchronous requests.

Maybe this can help you more about Volley and asyn/sync requests:

But normally you would send an async-request and when you get the response you do your logic (update fields, store something in database, ...).

TomStroemer
  • 1,390
  • 8
  • 28
1

Your computerServicesArrayList is populated by callback from Volley (new Response.Listener()). This population happens correctly as you have verified. But it does take some time, for the network up/down travel. When your MainActivity's call to mySQLConnect.getData() returns this round trip is not complete yet; so you get an empty list in MainActivity.

The usual solution to this problem is to make the listener call methods in MainActivity. This can be done by making

class MainActivity implements Response.Listener<String> {

/* --- */
@Override
public void onResponse(String response) {
    showJSON(response);
}

void showJSON(String response){
    // Do the stuff here
}
FreeBird
  • 691
  • 1
  • 8
  • 18