skip to Main Content

i have generated an app over e-commerce site (magento 2) while i am trying to startup my app it processing very slowly because of many products in my server is there any possible way to speed up my usage of Async task while using JSON feeds.. Please let me for any possible ways

My one of the AsyncTask coding:

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);

    }

}

Thank u in advance..

3

Answers


  1. Asyntask performance speed is too low compare to Retrofit.

    So For e-commerce app, You must use Retrofit.

    Login or Signup to reply.
  2. There are few things you need to update in your code

    1. API Calling lib: I’m using Retrofit for api calling very fast & simple to use. Support Header & Response caching too.
    2. JSON Parsing: You are parsing JSON manually which is a time-consuming process. I’m using Google’s JSON parsing Lib Gson. really very fast.
    3. Pagination: If you having lots of data on the server then try to fetch data in small no of pieces. For example in case of “Item Listing API” try to fetch data from the server for 10-15 item at a time rather all the item at a once.
    Login or Signup to reply.
  3. There are a few things you can do. For starters, you don’t have to parse the whole json, before updating view.
    But really you yourself state the issue, which is you have too much data. This is not only a programming issue, it is also a user experience issue, too much data is confusing, especially on a mobile device.
    What I suggest is breaking down your data into categories or the like. When app starts, download just a list of categories to display. Upon user choosing a category, then you download the data for that category.
    When you download data, do it in chunks, so that you can start displaying right away.
    There are many similar ideas that you can implement, for a better user experience.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search