我想从本地服务器获取工厂数据,而不知道工厂的确切数量。以下是一些代码:

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");

            }
        });
    }

onSucceed方法的getFactories中,有一行Log.d("wellS", String.valueOf(isSuccess)+ " ABC");,在do-while中执行此方法后,有Log.d("wellS", String.valueOf(isSuccess))。但Logcat显示:
2019-05-12 19:42:17.597 18535-18535/?D/井:假
2019-05-12 19:42:17.646 18535-18535/?D/Wells:开始从服务器获取数据
2019-05-12 19:42:17.845 18535-18535/?D/Wells:已成功连接到服务器
2019-05-12 19:42:17.846 18535-18535/?D/井:施工
2019-05-12 19:42:17.846 18535-18535/?钻井:真ABC
2019-05-12 19:42:17.846 18535-18535/?D/井:连接完成
第二行似乎是在第一行之前执行的,而do-while只执行了一次。为什么?

最佳答案

因为队列将在一个新的made线程中异步处理请求。这意味着它没有阻塞主线程的运行(包含2019-05-12 19:42:17.597 18535-18535/? D/wellS: false)。
服务器请求正在阻塞操作,事实上,它会阻塞正在运行的线程,直到收到响应。在分离线程中执行该操作有助于不阻塞父线程。

07-24 09:34