向arraylist添加数据时出现运行时错误

时间:2015-08-10 17:47:38

标签: java android

我的代码运行正常,但是当我将值1发送到subcat时,它不会显示数据,但会显示我发送5或9的时间。我的第一个想法是因为它的大小所以我删除并评论图像添加。但我仍然无法显示subcat=1的数据。

的AsyncTask

protected Void doInBackground(String... strings) {
        ArrayList<item> jsonArrayList = new ArrayList<item>();
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet httpget = new HttpGet("http://avdeal.in/get_all_products.php");
            HttpResponse response = client.execute(httpget);
            HttpEntity entity = response.getEntity();
            String data = EntityUtils.toString(entity);
            JSONObject jsonObject = new JSONObject(data);
            if (jsonObject != null) {
                String posts = jsonObject.getString("products");
                JSONArray postsArray = new JSONArray(posts);
                if (postsArray != null) {
                    if (postsArray.length() > 0) {
                        for (int i = 0; i < postsArray.length(); i++) {
                            JSONObject postsObject = postsArray
                                    .getJSONObject(i);
                            int subcat = postsObject.getInt("subcat_id");
                            if (subcats == 1) {
                                if (subcats == subcat) {
                                    String title = postsObject.getString("product_title");
                                    String price = postsObject.getString("product_price");
                                    String url = "http://avdeal.in/seller/upload_images/" + postsObject.getString("product_url");
                                    //InputStream is = (InputStream) new URL(url).getContent();
                                    //Bitmap b = BitmapFactory.decodeStream(is);
                                    list.add(new item(title, price, url));
                                }
                                } else  {
                            if (subcats == subcat) {
                                String title = postsObject.getString("product_title");
                                String price = postsObject.getString("product_price");
                                String url = "http://avdeal.in/seller/upload_images/" + postsObject.getString("product_url");

                                InputStream is = (InputStream) new URL(url).getContent();
                                Bitmap b = BitmapFactory.decodeStream(is);

                                list.add(new item(title, price, url, b));
                            }
                        }
                        }
                    }


                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

0 个答案:

没有答案