amitshekhariitbhu / Fast-Android-Networking

šŸš€ A Complete Fast Android Networking Library that also supports HTTP/2 šŸš€
https://outcomeschool.com
Apache License 2.0
5.68k stars 956 forks source link

Delay response in Fast-Android-Networking-the value playerUsername comes of response late here, thanks advance #544

Open ssoheily opened 3 years ago

ssoheily commented 3 years ago
   AndroidNetworking.get("http://192.168.188.42:7800/app/listuser")
            .setTag("listuser")
            .setPriority(Priority.MEDIUM)
            .build()
            .getAsJSONArray(new JSONArrayRequestListener() {
                                @Override
                                public void onResponse(JSONArray response) {
                                    //   listener.onRequestCompleted(response);
                                    try {
                                        // looping through All  player
                                        for (int i = 0; i < response.length(); i++) {
                                            JSONObject c = response.getJSONObject(i);
                                            //Log.d("username", c.getString("username"));
                                            String username = c.getString("username");
                                            int id=c.getInt("id");
                                            Playermodel playermodel=new Playermodel(id, username);

/// *** playerUsername.add(playermodel); }

                                    } catch (JSONException e) {

                                        e.printStackTrace();
                                    }
                                }

                                @Override
                                public void onError(ANError anError) {
                                    Log.i("Get_Error: ", anError.toString());
                                }
                            }
            );

//??????????????????? please help: I do not ,what to do??????? // the value playerUsername comes of response, but because the value playerUsername is late here and causes the value of Y to //be null final ArrayList Y = playerUsername; PlayerRepo playerRepo=new PlayerRepo(this); final ArrayList items3 = new ArrayList<>(); //choise items for play(complete prscess): for(int i=0 ; i<Y.size();i++) { items3.add(Y.get(i).getUsername()); } final String emp = "Please select a player for the tourney:"; items3.add(0, emp); final ArrayAdapter adapter2 = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, items3); ``

heskadon commented 3 years ago

Yes because it's an asynchronous process. You can use this fast android networking with synchronous one or move your logic inside onResponse.