簡體   English   中英

Java:數組未接收到值

[英]Java: array not receiving values

這是代碼。 list是一個全局變量。

    ArrayList<LatLng> list = new ArrayList<LatLng>();

    public void addCoordinate(double a, double b){
        list.add(new LatLng(a,b));
    }

public ArrayList<LatLng> readItems(int ignore) throws JSONException {
    addCoordinate(2.0,2.0);


    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
            showUrl, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            System.out.println(response.toString());
            try {
                JSONArray records = response.getJSONArray("locations");
                for (int i = 0; i < records.length(); i++) {
                    JSONObject locations = records.getJSONObject(i);

                    double latitude = locations.getDouble("Latitude");
                    double longitude = locations.getDouble("Longitude");
                   // double lat = Double.parseDouble(latitude);
                   // double lng = Double.parseDouble(longitude);
                    list.add(new LatLng(latitude,longitude));
                }
                addCoordinate(3.0,3.0);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            System.out.append(error.getMessage());

        }
    });
    //requestQueue.add(jsonObjectRequest);

    addCoordinate(1.0,1.0);
    return list;
}

它僅在調用getCoordinate時返回兩個值(1,1和2,2),但會跳過(3,3)。 如果有人有修復程序,我將不勝感激。

(3,3)僅在調用onResponse之后才會添加。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM