代码之家  ›  专栏  ›  技术社区  ›  udhaya

如何在使用来自服务器的json提要时快速生成我的android应用程序

  •  1
  • udhaya  · 技术社区  · 7 年前

    我在电子商务网站(magento 2)上生成了一个应用程序,当我试图启动我的应用程序时,它的处理速度非常慢,因为我的服务器中有许多产品。在使用JSON提要时,有没有任何可能的方法来加快我使用异步任务的速度。。请告诉我任何可能的方法

    我的异步任务编码之一:

    private class GetProduct extends AsyncTask<Void, Void, Void> {
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            dialog_pro = new ProgressDialog(Healthy_Cat.this);
            dialog_pro.setMessage("Please wait...");
            dialog_pro.setCancelable(false);
            dialog_pro.show();
    
        }
    
        @Override
        protected Void doInBackground(Void... arg0) {
            HttpHandler sh = new HttpHandler();
            String jsonStr = sh.makeServiceCall(url);
    
    
    
            if (jsonStr != null) {
                try {
    
    
                    JSONArray items = new JSONArray(jsonStr);
                    for (int i = 0; i < items.length(); i++) {
                        JSONObject c = items.getJSONObject(i);
                        pro_name = c.getString("name");
                        String price = c.getString("price");
                        JSONArray array = c.getJSONArray("custom_attributes");
                        for (int k = 0; k < array.length(); k++) {
                            JSONObject jb = array.getJSONObject(k);
                            String attr = jb.getString("attribute_code");
    
                            if (attr.equalsIgnoreCase("special_price")) {
    
                                splprice = jb.getString("value");
    
                            }
                        }
    
    
                        String sku = c.getString("sku");
    
                        JSONArray media = c.getJSONArray("media_gallery_entries");
    
                        for(int k = 0; k < media.length(); k++) {
                            JSONObject jb = media.getJSONObject(k);
    
                            String imageURL =  BaseURL_Helper.MediaBase +jb.getString("file");
    
                            media_image = imageURL;
    
                            // tmp hash map for single contact
                            Beanclass dataSet = new Beanclass();
                            dataSet.setTitle(pro_name);
                            dataSet.setImage(imageURL);
                            dataSet.setPrice(price);
                            dataSet.setSPLPrice(splprice);
                            dataSet.setSku(sku);
                            list.add(dataSet);
    
                            BeanclassList data = new BeanclassList();
                            data.setTitle(pro_name);
                            data.setImage(imageURL);
                            data.setSku(sku);
                            data.setSPLPrice(splprice);
                            data.setPrice(price);
                            listbean.add(data);
    
                        }
    
                    }
                }catch (final JSONException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            no_list.setVisibility(View.VISIBLE);
    
                        }
                    });
    
                }
            } else {
    
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "May be Network error!!",
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });
    
            }
    
            return null;
        }
    
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            /**
             * Updating parsed JSON data into ListView
             * */
            if (dialog_pro.isShowing())
                dialog_pro.dismiss();
    
            mAdapter = new GridviewAdapter(Healthy_Cat.this,list);
            gridlist.setAdapter(mAdapter);
    
            Listadapter = new ListviewAdapter(Healthy_Cat.this,listbean);
            listview_pro.setAdapter(Listadapter);
    
        }
    
    }
    

    3 回复  |  直到 7 年前
        1
  •  0
  •   Rahul Pareta    7 年前

    代码中几乎没有需要更新的内容

    1. API调用库:我正在使用 Retrofit 对于api调用,速度非常快&使用简单。支架收割台;响应缓存。
    2. JSON解析:您正在手动解析JSON,这是一个耗时的过程。我正在使用谷歌的JSON解析库 Gson . 真的很快。
        2
  •  0
  •   Arjun Solanki    7 年前

    与改装相比,Asyntask性能速度太低。

        3
  •  0
  •   lionscribe    7 年前



    我的建议是将您的数据分解为类别或类似的内容。当应用程序启动时,只下载要显示的类别列表。用户选择一个类别后,就可以下载该类别的数据。
    下载数据时,要分块进行,这样就可以立即开始显示。