首页 > 解决方案 > 第一个对象未​​添加到 arraylist

问题描述

我有一个存储不同属性的类 city 和一个发送的方法http request

 public class city {
    int temperature;
    String conditions;
    String town;
    String country;
    String state;

    public String requestData(String url) {

        RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObj = new JSONObject(response);
                            JSONObject mresponse = jsonObj.getJSONObject("query");
                            cityIndex = mresponse.optJSONObject("results").optJSONObject("channel").optJSONObject("location").getString("city");
                            SharedPreferences cityList = getSharedPreferences("cityList", MODE_PRIVATE);
                            SharedPreferences.Editor editor = cityList.edit();
                            editor.putString(cityIndex, response);
                            editor.apply();
                        }catch (JSONException e) {
                            e.printStackTrace();
                        }

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

        queue.add(stringRequest);
        return null;
    }

}

然后,我在将邮政编码输入文本字段后运行这行代码,该邮政编码采用邮政编码,将其包装在 url 中,然后将 url 作为 http 请求发送到city.requestData(). 然后将 json 字符串化并使用 sharedpreferences 发回。然后将 json 制成一个对象,然后将其存储在新创建的城市对象的不同属性中。然后将这些创建的城市存储在 citylist 数组中。

       Editable zipCode = mCity.getText();
                String API = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D" + String.valueOf(zipCode) + ")&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
                city createdCity = new city();
                createdCity.requestData(API);
                dialog.dismiss();
                SharedPreferences cityList = getSharedPreferences("cityList", MODE_PRIVATE);
                String mResponse= cityList.getString(cityIndex, "");

                try {
                    JSONObject jsonObj = new JSONObject(mResponse);
                    JSONObject response = jsonObj.getJSONObject("query");
                    createdCity.temperature = response.optJSONObject("results").optJSONObject("channel").optJSONObject("item").optJSONObject("condition").getInt("temp");
                    createdCity.conditions = response.optJSONObject("results").optJSONObject("channel").optJSONObject("item").optJSONObject("condition").getString("text");
                    createdCity.town = response.optJSONObject("results").optJSONObject("channel").optJSONObject("location").getString("city");
                    createdCity.country = response.optJSONObject("results").optJSONObject("channel").optJSONObject("location").getString("country");
                    createdCity.state = response.optJSONObject("results").optJSONObject("channel").optJSONObject("location").getString("region");
                   cities.add(createdCity);
                    addMenuItemInNavMenuDrawer(createdCity.town , createdCity.state);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Log.d("cities", cities.toString());
                i++;

我的代码遇到的问题是在记录“城市”时,第一个城市从未被记录,第二个城市被记录为第一个城市应该是什么。

例如,如果我输入 Kansas,它将被记录为 [ ]。然后我输入纽约,它将记录堪萨斯。然后我输入洛杉矶,它会注销纽约。

有人知道为什么吗?

标签: javaandroid

解决方案


您似乎在城市完成其请求之前检索共享偏好。请记住,网络请求是在后台线程中发出的。当城市仍在请求数据时,您读取了前一个城市存储的首选项。


推荐阅读