khirulnizam

Training Listing JSON without search

Feb 6th, 2018
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. package my.edu.kuis.listingjson;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.os.AsyncTask;
  5. import android.os.Bundle;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.util.Log;
  8. import android.widget.ListAdapter;
  9. import android.widget.ListView;
  10. import android.widget.SimpleAdapter;
  11. import android.widget.Toast;
  12. import org.json.JSONArray;
  13. import org.json.JSONException;
  14. import org.json.JSONObject;
  15. import java.util.ArrayList;
  16. import java.util.HashMap;
  17. public class Listing extends AppCompatActivity {
  18. private String TAG = Listing.class.getSimpleName();
  19. private ProgressDialog pDialog;
  20. private ListView lv;
  21. // URL to get contacts JSON
  22. ArrayList<HashMap<String, String>> traininglist;
  23. String searchkey;
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_listing);
  28. //Bundle extras = getIntent().getExtras();
  29. //searchkey=extras.getString("searchkey").toString();
  30. //rename actionbar title to search keyword
  31. setTitle(searchkey+" Trainings");
  32. //URL with searchkey
  33. traininglist = new ArrayList<>();
  34. lv = (ListView)findViewById(R.id.list);
  35. new GetTraining().execute();
  36. }
  37. /**
  38. * Async task class to get json by making HTTP call
  39. */
  40. private class GetTraining extends AsyncTask<Void, Void, Void> {
  41. //Log.e(url2);
  42. @Override
  43. protected void onPreExecute() {
  44. super.onPreExecute();
  45. // Showing progress dialog
  46. pDialog = new ProgressDialog(Listing.this);
  47. pDialog.setMessage("Please wait...");
  48. pDialog.setCancelable(false);
  49. pDialog.show();
  50. }
  51. @Override
  52. protected Void doInBackground(Void... arg0) {
  53. HttpHandler sh = new HttpHandler();
  54. //set the URL address of the JSON service page here
  55. String url2 = "http://khirulnizam.com/training/search.php";
  56. Log.e(TAG,url2);
  57. // Making a request to url and getting response
  58. String jsonStr = sh.makeServiceCall(url2);
  59. Log.e(TAG, "Response from url: " + jsonStr);
  60. //if respon null
  61. if (jsonStr != null) {
  62. try {
  63. JSONObject jsonObj = new JSONObject(jsonStr);
  64. // Getting JSON Array node name
  65. JSONArray contacts = jsonObj.getJSONArray("traininglist");
  66. // looping through All Contacts
  67. for (int i = 0; i < contacts.length(); i++) {
  68. JSONObject c = contacts.getJSONObject(i);
  69. String id = c.getString("id");
  70. String trainingname = c.getString("trainingname");
  71. String website = c.getString("website");
  72. // tmp hash map for single contact
  73. HashMap<String, String> contact = new HashMap<>();
  74. // adding each child node to HashMap key => value
  75. contact.put("id", id);
  76. contact.put("trainingname", trainingname);
  77. contact.put("website", website);
  78. // adding contact to contact list
  79. traininglist.add(contact);
  80. }
  81. //if JSON format error
  82. } catch (final JSONException e) {
  83. Log.e(TAG, "Json parsing error: " + e.getMessage());
  84. runOnUiThread(new Runnable() {
  85. @Override
  86. public void run() {
  87. Toast.makeText(getApplicationContext(),
  88. "Json parsing error: " + e.getMessage(),
  89. Toast.LENGTH_LONG)
  90. .show();
  91. }
  92. });
  93. }
  94. } else {
  95. // if JSON service server not responding
  96. Log.e(TAG, "Couldn't get json from server.");
  97. runOnUiThread(new Runnable() {
  98. @Override
  99. public void run() {
  100. Toast.makeText(getApplicationContext(),
  101. "Couldn't get json from server. Check LogCat for possible errors!",
  102. Toast.LENGTH_LONG)
  103. .show();
  104. }
  105. });
  106. }
  107. return null;
  108. }
  109. @Override
  110. protected void onPostExecute(Void result) {
  111. super.onPostExecute(result);
  112. // Dismiss the progress dialog
  113. if (pDialog.isShowing())
  114. pDialog.dismiss();
  115. /**
  116. * Updating parsed JSON data into ListView
  117. * */
  118. ListAdapter adapter = new SimpleAdapter(
  119. Listing.this, traininglist,
  120. R.layout.list_item, //list_item.xml
  121. new String[]{"id", "trainingname", "website"},//array list
  122. new int[]{R.id.id, R.id.trainingname, R.id.website}//the UIs in list_item.xml
  123. );
  124. lv.setAdapter(adapter);
  125. }
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment