繁体   English   中英

无法将json值打印到整数数组列表? 在那里保存但无法退出循环?

[英]Unable to print json value to a integer array list? is saves there but unable to take out off the loop?

谁能帮我把arraylist出循环。 如果我将其取出,则它作为数组越界越空。 这是我的代码:

public void collapse() {
    corequestQueue= Volley.newRequestQueue(getApplicationContext());
    String url="http://192.168.43.208/online/json.php";
    JsonObjectRequest jsonObjectRequest3=new JsonObjectRequest(Request.Method.GET, url,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray jsonArray=response.getJSONArray("server_responce");
                    for (int i=0;i<=jsonArray.length();i++){

                        JSONObject admin=jsonArray.getJSONObject(i);

                        solution.add(Integer.valueOf(admin.getString("sno")));

                    //if i do like this the values prints here in the toast.        
                    //Toast.makeText(User_Login.this, "hello"+solution.get(0), Toast.LENGTH_SHORT).show();
                    //printing the toast anywhere out of the block gives error.. as assay out of bound 0 on index size 0.. help me guys
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("Volly","error");
            }
        }

        );
    corequestQueue.add(jsonObjectRequest3);
    Toast.makeText(User_Login.this, "hello"+solution.get(0), Toast.LENGTH_SHORT).show();
}

最后一行给我一个错误,我无法将数组列表带到外面的任何地方,因为它出来的是空的。 当它在循环内并且向其添加值时,它是完美的。 我已经检查过了,唯一的事情是我不能取出它吗?

期望您已全局给定了Integer类型的ArrayList。

public void collapse(){
solution = new ArrayList<>();
corequestQueue= Volley.newRequestQueue(getApplicationContext());
String url="http://192.168.43.208/online/json.php";
JsonObjectRequest jsonObjectRequest3=new JsonObjectRequest(Request.Method.GET, url,
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray jsonArray=response.getJSONArray("server_responce");
                for (int i=0;i<=jsonArray.length();i++){

                    JSONObject admin=jsonArray.getJSONObject(i);
                    String val=admin.optString("sno"));
                    if(!val.equalIgnoreCase(""))
                    solution.add(Integer.parseInt(val);

                    //if i do like this the values prints here in the toast.        

                    //printing the toast anywhere out of the block gives error.. as assay out of bound 0 on index size 0.. help me guys
                }
    Toast.makeText(User_Login.this, "hello"+solution.get(0), Toast.LENGTH_SHORT).show();
              if(solution.size()!=0)
               { 
               Collections.shuffle(solution) 
               }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Volly","error");
        }
    }
);
corequestQueue.add(jsonObjectRequest3);

}

凌空的仓库只会在互联网的确定时间bz之后给出详细信息,所以我已经使用处理程序..来调用我应该执行的肉类程序。

     new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(User_Login.this, ""+solution.size(), Toast.LENGTH_SHORT).show();
            Collections.shuffle(solution);
            data();
        }
    },i*3000);

我的将json添加到数组列表的代码:我可以设置是否检查数组列表大小的条件来知道哪里有合适的互联网。

            try {
                        JSONArray jsonArray=response.getJSONArray("server_responce");
                        for (int i=0;i<=jsonArray.length();i++){

                            JSONObject admin=jsonArray.getJSONObject(i);
                            String bno = admin.getString("bno");
                            String series=admin.getString("sno");
                            if (bnum.equals(bno)) {
                                int k = Integer.valueOf(series);
                                solution.add(k);
                            }

                        }

确实,Rushi Ayyappa发布了解决方案。 我要再次发布。

public void collapse() {
    corequestQueue= Volley.newRequestQueue(getApplicationContext());
    String url="http://192.168.43.208/online/json.php";
    JsonObjectRequest jsonObjectRequest3=new JsonObjectRequest(Request.Method.GET, url,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray jsonArray=response.getJSONArray("server_responce");
                    for (int i=0;i<=jsonArray.length();i++){

                        JSONObject admin=jsonArray.getJSONObject(i);

                        solution.add(Integer.valueOf(admin.getString("sno")));

                    //if i do like this the values prints here in the toast.        
                    //Toast.makeText(User_Login.this, "hello"+solution.get(0), Toast.LENGTH_SHORT).show();
                    //printing the toast anywhere out of the block gives error.. as assay out of bound 0 on index size 0.. help me guys
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                handleAfterResponse();
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("Volly","error");
            }
        }

        );
    corequestQueue.add(jsonObjectRequest3);
 }

private void handleAfterResponse() {
 if(solution != null && !solution.isEmpty()) {
 Toast.makeText(User_Login.this, ""+solution.size(), Toast.LENGTH_SHORT).show();

            Collections.shuffle(solution);
            data();
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM