在android中解析大量的json数据(一次大约10k记录)

时间:2017-03-29 10:35:21

标签: android json

目前我正在使用Android应用程序,我必须解析10k  来自服务器的产品信息,并在Searchable中显示该数据  微调。  它在使用wifi时工作正常,但在移动数据上,它需要很长时间才能解析产品信息。 我已经使用loopj库进行json解析。

那么,有人能建议我更好的方法吗?

这是我的onSuccess方法,我试图解析大数据集并将其放入Searchable微调器中,以便用户可以搜索并从所有数据中选择产品。

@Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                    super.onSuccess(statusCode, headers, response);
                    //System.out.println("Response " + response.toString());
                    try {
                        if (response.has("products_data")) {

                        jsonArray = new JSONArray();
                        jsonArray = response.getJSONArray("products_data");
                        for (int t = 0; t < jsonArray.length(); t++) {
                            object = jsonArray.getJSONObject(t);
                            retailer_id = object.getString("retailer_id");
                            retailer_name = object.getString("retailer_name");
                            product_code = object.getString("product_code");
                            product_name = object.getString("product_name");
                            pro_packing = object.getString("pro_packing");
                            pro_company=object.getString("pro_company");
                            pro_generic_code=object.getString("pro_generic_code");
                            pro_generic_name=object.getString("pro_generic_name");
                            pro_stock = object.getInt("product_stock");
                            product_MRP=object.getDouble("product_MRP");
                            p = new Product();
                            p.setRetailer_id(retailer_id);
                            p.setRetailer_name(retailer_name);
                            p.setProduct_code(product_code);
                            p.setProduct_name(product_name);
                            p.setPro_packing(pro_packing);
                            p.setPro_company(pro_company);
                            p.setPro_generic_code(pro_generic_code);
                            p.setPro_generic_name(pro_generic_name);
                            p.setPro_stock(pro_stock);
                            p.setProduct_MRP(product_MRP);
                            productlist.add(p);
                        }


       progressDialog.dismiss();
                   // Log.i("PROLIST Size:: ", String.valueOf(productlist.size()));

                    final List<String> pname = new ArrayList<String>();
                    final List<String> pcode = new ArrayList<String>();
                    pname.clear();
                    pname.add(0,"Select Products");
                    for (int i = 0; i < productlist.size(); i++) {
                        pname.add(productlist.get(i).getProduct_name());
                    }
                    // Creating adapter for spinner
                    final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.custom_spinnertxt, pname);
                    // Drop down layout style - list view with radio button
                    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // attaching data adapter to spinner

                            autoCompleteTextView.setAdapter(spinnerAdapter);
                            autoCompleteTextView.setSelection(0);
                        }
                    });

                    autoCompleteTextView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int i, long id) {
                            if(i>0) {
                                p_id = productlist.get(i - 1).getProduct_code();
                                p_stock = productlist.get(i - 1).getPro_stock();
                                ppack = productlist.get(i - 1).getPro_packing();
                                pcomp = productlist.get(i - 1).getPro_company();
                                mrp = productlist.get(i - 1).getProduct_MRP();
                                pseller = productlist.get(i - 1).getRetailer_name();
                               // Log.i("pID:", p_id);
                                //Log.i("pStock:", String.valueOf(p_stock));
                                //Log.i("PMRP:", String.valueOf(mrp));
                            }
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {

                        }
                    });

                }

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

            }

        }

0 个答案:

没有答案