Search code examples
androidandroid-volley

Android - Wait for Volley response to complete and continue executing


I need execute a Volley request and wait for the response to parse it and return it, but have no idea on how to achieve this. Can someone help? here is my code :

public void getWord(){
    final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://ent-ifsi.com/Projet/Application_Android/pendu_android.php",
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    try {
                        JSONObject lesMots = response.getJSONObject("lesMots");

                        Iterator<?> keys = lesMots.keys();

                        while (keys.hasNext()) {
                            String key = (String) keys.next();
                            if (lesMots.get(key) instanceof JSONObject) {
                                JSONObject obj = (JSONObject) lesMots.get(key);
                                //Récupération des deux mots, anglais et français
                                String wordToFind = obj.getString("wordFrench").toUpperCase();
                                String wordEnglish = obj.getString("wordEnglish").toUpperCase();
                                //Mots mis dans un array respectif

                                listOfWordEnglish.add(wordEnglish);
                                listOfWordFrench.add(wordToFind);
                                container.removeAllViews();
                            }
                            numberOfWord++;

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(), e + "", Toast.LENGTH_LONG).show();
                    }

                }
            },


            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("Volley", "ERROR");
                    Toast.makeText(getApplicationContext(), error + "", Toast.LENGTH_LONG).show();

                }
            }


    );
    queue.add(jsonObjectRequest);

}

There is 7 word in each array but when i'm trying to get acess into my initGame function, it said they are empty, i guess it's because volley request is not this fast ! if someone can explain me how to do that

here is my initGame function

`public void initGame() {
        win = false;
        found = 0;
        typed_word.setText("");
        image.setBackgroundResource(R.drawable.first);
        listOfLetters = new ArrayList<>();
        listOfWordFrench = new ArrayList<>();
        listOfWordEnglish = new ArrayList<>();
        getWord();

        for (int i = 0; i < listOfWordEnglish.size(); i++){
            System.out.println(i);
            String wordEnglish = listOfWordEnglish.get(i);
            wordEnglishPendu.setText(wordEnglish);
            word = listOfWordFrench.get(i);
            for (int x = 0; x < word.length(); x++) {
                TextView oneLetter = (TextView) getLayoutInflater().inflate(R.layout.textview, null);
                container.addView(oneLetter);

            }
        }
    }`

Solution

  • Since Volley is asynchronous, so the for loop below getWord() will be called before onResponse of your Volley request.

    The solution for your issue is to move that for loop into onResponse, there you can place it after the while loop.