1

I'm working on a App that should get a JSON response from a webservice and write every element in a listview(Image and Text), I did work with AsyncTask to get the HTTP Response to retrieve data from the webservice and display them in TextViews and ImageViews. But when I try to display listview, it doesn't display anything(Text & Images).

LogCat:

03-18 09:13:36.329: I/Process(1478): Sending signal. PID: 1478 SIG: 9
03-18 09:15:16.279: I/dalvikvm-heap(1547): Grow heap (frag case) to 4.198MB for 1127536-byte allocation
03-18 09:15:17.179: I/Choreographer(1547): Skipped 46 frames!  The application may be doing too much work on its main thread.
03-18 09:15:17.619: I/Choreographer(1547): Skipped 30 frames!  The application may be doing too much work on its main thread.
03-18 09:15:19.039: I/Choreographer(1547): Skipped 131 frames!  The application may be doing too much work on its main thread.
03-18 09:15:25.309: I/Choreographer(1547): Skipped 40 frames!  The application may be doing too much work on its main thread.
03-18 09:15:27.749: I/Choreographer(1547): Skipped 79 frames!  The application may be doing too much work on its main thread.
03-18 09:15:32.999: I/Choreographer(1547): Skipped 34 frames!  The application may be doing too much work on its main thread.
03-18 09:15:47.619: I/dalvikvm-heap(1547): Grow heap (frag case) to 5.085MB for 498256-byte allocation
03-18 09:15:48.299: I/Choreographer(1547): Skipped 30 frames!  The application may be doing too much work on its main thread.
03-18 09:17:02.299: I/Choreographer(1547): Skipped 52 frames!  The application may be doing too much work on its main thread.
03-18 09:18:28.929: I/Choreographer(1547): Skipped 32 frames!  The application may be doing too much work on its main thread.

MainActivity:

public class MainActivity extends Activity {

private ProgressDialog pDialog;

// URL to get contacts JSON
private static String url = "MY_URL";

// JSON Node names
public static final String TAG_CONTACTS = "contacts";
public static final String TAG_NAME = "name";
public static final String TAG_PRODUCT_IMG = "product_img";
ArrayList<HashMap<String, String>> arraylist;

JSONObject JOBJ;
JSONArray contacts = null;

ListViewAdapter adapter;
ListView lv;

public JSONArray jsonStr;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    lv = (ListView) findViewById(R.id.list);
    new GetContacts().execute();
}

private class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        arraylist = new ArrayList<HashMap<String, String>>();
        JOBJ = JSONfunctions
                .getJSONfromURL("http://www.nopaltechnologies.com/restaurant/products.php/");

        try {

            jsonStr = JOBJ.getJSONArray(TAG_CONTACTS);
            // Getting JSON Array node

            // looping through All Contacts
            for (int i = 0; i < jsonStr.length(); i++) {

                // tmp hashmap for single contact
                HashMap<String, String> contact = new HashMap<String, String>();
                JOBJ = jsonStr.getJSONObject(i);
                // adding each child node to HashMap key => value
                contact.put(TAG_PRODUCT_IMG, JOBJ.getString("product_img"));
                contact.put(TAG_NAME, JOBJ.getString("name"));

                // adding contact to contact list
                arraylist.add(contact);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        adapter = new ListViewAdapter(MainActivity.this, arraylist);
        lv.setAdapter(adapter);
        pDialog.dismiss();
    }

}
  }
Tushar Gogna
  • 4,963
  • 6
  • 31
  • 42
Sunishtha Singh
  • 321
  • 4
  • 17
  • @Raghunandan can you help me with code problem? I trd to do they way you edited ques, anyways thanks. – Sunishtha Singh Mar 19 '15 at 05:56
  • 1
    possible duplicate of [The application may be doing too much work on its main thread](http://stackoverflow.com/questions/14678593/the-application-may-be-doing-too-much-work-on-its-main-thread) – default locale Mar 19 '15 at 05:58
  • @Sunishtha Singh There is no chance for such a warning according to your code snippet – Don Chakkappan Mar 19 '15 at 05:59
  • 2
    Can't say for sure, but it could be that gc is being very active and halting the whole app (including UI) as you allocate and use large short-term objects. Choreographer is only complaining because it's not getting enough CPU time. See above comment from @defaultlocale for the general reasons for "too much work" logcat messages. – initramfs Mar 19 '15 at 05:59
  • It is recommended to use java threads for excessive CPU loads than android native Asynctask. – Hulk Mar 19 '15 at 06:01
  • 1
    Add some check in your onPostExecute() ; whether there is any data to show , otherwise add some dump data to arraylist (Say "No data found") & set the adapter – Don Chakkappan Mar 19 '15 at 06:02
  • CAN YOU PLEASE DISMISS DIALOGE BEFORE SET ADAPTER – Digvesh Patel Mar 19 '15 at 06:05
  • What would that do? @DigveshPatel – Tushar Gogna Mar 19 '15 at 06:42
  • In main thread i, its doing too much work. Is There is any problem with that or do i need to change some. – Sunishtha Singh Mar 19 '15 at 06:45
  • Well, you are parsing the JSON Array into a new Array. How about letting the adapter handle the JSON Array directly and skip the ArrayList> that does not add any functionality? – mach Mar 19 '15 at 07:07
  • 1
    Make a pojo class having the same fields as in the Json and then add those items in your arraylist. Secondly, the images seems to be heavy, add `largeHeap="true"` property in your manifest.xml. – Tushar Gogna Mar 19 '15 at 07:09

0 Answers0