【问题标题】:Android JSONArray length value 0Android JSONArray 长度值 0
【发布时间】:2018-03-18 08:57:06
【问题描述】:

我正在尝试获取用于微调器的 JSONArray 长度值,但即使微调器的值大于 0,它也始终返回 0。这是我的代码

类 ConfigJSON

public class ConfigJSON {
      public static int value;
      public static final String JSON_ARRAY = "result";
}

主要活动

protected void onCreate{
    getData();
    new UpdateUser.GetUserInfo(UpdateUser.this).execute();
}

public void getData() {
    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            JSONObject j = null;
            try {
                j = new JSONObject(response);
                result = j.getJSONArray(ConfigJSON.JSON_ARRAY);
                ConfigJSON.value = result.length();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

private class GetUserInfo extends AsyncTask<Void, Void, Void> {

    //Do Stuff before PostExecute

    @Override
    protected void onPostExecute(Void result) {

        //The textview will output the JSONArray length
        TextView msg = (TextView) findViewById(R.id.txtmsg);
        msg.setText(Integer.toString(ConfigJSON.value));

        int index;
        for (int i=0;i<ConfigJSON.value;i++){
            if (spinner.getItemAtPosition(i).toString().equals("stuff")){
                index = i;
                spinner.setSelection(index);
            }
        }
    }
}

这段代码的重点是获取 JSONArray 值,这样我就可以在微调器中设置一个循环,但该值始终为 0,因此微调器将始终选择第一个值。我像这样更改循环中的值以测试 setSelection 是否损坏

for (int i=0;i<3;i++){
         if (spinner.getItemAtPosition(i).toString().equals("stuff")){
           index = i;
             spinner.setSelection(index);
         }
}

微调器工作正常,所以我删除了

new UpdateUser.GetUserInfo(UpdateUser.this).execute();

在onCreate中并放入getData

public void getData() {
    //Same as the getData above but only this line at the end
    new UpdateUser.GetUserInfo(UpdateUser.this).execute();
}

因为我想确保 GetUserInfo 将在设置 getData 的值后运行。但即使 getData 中有 3 个值,它仍然返回 0。那么如何获取 result.length() 值并在 GetUserInfo 中使用呢?

【问题讨论】:

  • 那么这里的string 是什么?
  • 这是一些东西,所以我可以与微调器值进行比较,如果为真,则选择该微调器值。我会编辑它,这样人们就不会再问这个问题了

标签: android arrays json


【解决方案1】:

我想确保 GetUserInfo 将在来自的值之后运行 getData 已设置

Volley 网络调用是异步的,所以在网络调用完成后执行任务,如

public void getData() {
    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            JSONObject j = null;
            try {
                j = new JSONObject(response);
                result = j.getJSONArray(ConfigJSON.JSON_ARRAY);
                ConfigJSON.value = result.length();
                new UpdateUser.GetUserInfo(UpdateUser.this).execute();
                //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

【讨论】:

  • 很高兴能帮上忙,编码愉快
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-22
  • 2012-09-02
  • 1970-01-01
  • 1970-01-01
  • 2018-01-05
  • 1970-01-01
  • 2014-07-27
相关资源
最近更新 更多