Advertisement
Guest User

Untitled

a guest
Aug 21st, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. // Hashmap for ListView
  2. ArrayList<HashMap<String, String>> all_itemList;
  3.  
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main_helloballi);
  8.  
  9. all_itemList = new ArrayList<HashMap<String, String>>();
  10. // Calling async task to get json
  11. new getAllItem().execute();
  12. }
  13.  
  14.  
  15. private class getAllItem extends AsyncTask<Void, Void, Void> {
  16.  
  17.  
  18. @Override
  19. protected Void doInBackground(Void... arg0) {
  20. // Creating service handler class instance
  21. ServiceHandler sh = new ServiceHandler();
  22.  
  23. // Making a request to url and getting response
  24. String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
  25.  
  26. Log.d("Response: ", "> " + jsonStr);
  27.  
  28. if (jsonStr != null) {
  29. try {
  30.  
  31. all_item = new JSONArray(jsonStr);
  32.  
  33.  
  34. // looping through All Contacts
  35. for (int i = 0; i < all_item.length(); i++) {
  36. JSONObject c = all_item.getJSONObject(i);
  37.  
  38. String item_id = c.getString(TAG_ITEM_ID);
  39. String category_name = c.getString(TAG_CATEGORY_NAME);
  40. String item_name = c.getString(TAG_ITEM_NAME);
  41.  
  42. // tmp hashmap for single contact
  43. HashMap<String, String> allItem = new HashMap<String, String>();
  44.  
  45. // adding each child node to HashMap key => value
  46. allItem.put(TAG_ITEM_ID, item_id);
  47. allItem.put(TAG_CATEGORY_NAME, category_name);
  48. allItem.put(TAG_ITEM_NAME, item_name);
  49.  
  50. // adding contact to contact list
  51. all_itemList.add(allItem);
  52.  
  53. }
  54. } catch (JSONException e) {
  55. e.printStackTrace();
  56. }
  57. } else {
  58. Log.e("ServiceHandler", "Couldn't get any data from the url");
  59. }
  60.  
  61. return null;
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement