I wanted to get factory data from my local server without knowing the exact number of factories. Here are some codes:

public class StartActivity extends AppCompatActivity implements View.OnClickListener{
    boolean isSuccess = false;
    RequestQueue queue =NoHttp.newRequestQueue();
    ArrayList<String> fac_name = new ArrayList<>();
    ArrayList<String> lat = new ArrayList<>();
    ArrayList<String> lng = new ArrayList<>();

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //...
        NoHttp.initialize(this);
        //...
        do {
            int id = 1;
            getFactories(id);
            id++;
            Log.d("wellS", String.valueOf(isSuccess));

        }while(isSuccess);
    }

    public void getFactories(final int id){
        isSuccess = false;
        String url ="http://192.168.0.108:5000/factories/"+id;
        Request<JSONObject> request = NoHttp.createJsonObjectRequest(url);

        queue.add(0, request, new OnResponseListener<JSONObject>() {
            @Override
            public void onStart(int what) {
                Log.d("wellS", "Started to get data from server");
            }

            @Override
            public void onSucceed(int what, Response<JSONObject> response) {
                if(response.responseCode() == 200){
                    Log.d("wellS", "Successfully connected to server");
                    JSONObject j1 = response.get();
                    Iterator<String> it = j1.keys();

                    String key = it.next();
                    String value = null;
                    try {
                        value = j1.getString(key);
                        JSONObject j2 = new JSONObject(value);
                        Iterator<String> it2 = j2.keys();
                        int count = 1;
                        while(it2.hasNext()) { 
                            String key2 = it2.next();
                            String value2 = j2.getString(key2);

                            switch (count) {
                                case 1:
                                    fac_name.add(value2);
                                    break;
                                case 3:
                                    lat.add(value2);
                                    break;
                                case 4:
                                    lng.add(value2);
                                    break;
                                default:
                                    break;

                            }
                            count++;
                        }
                        Log.d("wellS", "工厂名称:" + fac_name.get(id-1)+" 纬度:"+lat.get(id-1)+" 经度:"+lng.get(id-1));
                        isSuccess = true;
                        Log.d("wellS", String.valueOf(isSuccess)+ " ABC");


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


                }
            }

            @Override
            public void onFailed(int what, Response<JSONObject> response) {

                Exception exception = response.getException();
                if(exception instanceof NetworkError) {
                    Log.d("wellS", "Error");
                }
            }

            @Override
            public void onFinish(int what) {
                Log.d("wellS", "Connection finished");

            }
        });
    }

In onSucceed of getFactories method, there's a line Log.d("wellS", String.valueOf(isSuccess)+ " ABC");, and after executing this method in do-while, there is Log.d("wellS", String.valueOf(isSuccess)). But the logcat shows:

2019-05-12 19:42:17.597 18535-18535/? D/wellS: false

2019-05-12 19:42:17.646 18535-18535/? D/wellS: Started to get data from server

2019-05-12 19:42:17.845 18535-18535/? D/wellS: Successfully connected to server

2019-05-12 19:42:17.846 18535-18535/? D/wellS: 工厂名称:南京 纬度:32.013521 经度:118.776431

2019-05-12 19:42:17.846 18535-18535/? D/wellS: true ABC

2019-05-12 19:42:17.846 18535-18535/? D/wellS: Connection finished

It seems like the second line was executed before the first one and do-while was executed only once. Why is that?

1 Answers

0
Yassin Hajaj On Best Solutions

Well, because the queue will treat the requests asynchronously in a new made Thread. Meaning that it is not blocking the running of the Main thread (containing the 2019-05-12 19:42:17.597 18535-18535/? D/wellS: false).

Server requests are blocking operations, in the fact that it blocks the thread it is running in until a response is received. Performing the operation in an apart thread helps not blocking the parent thread.