Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. package com.example.chung.myapplication;
  2. import android.app.ProgressDialog;
  3. import android.content.Intent;
  4. import android.os.AsyncTask;
  5. import android.os.Bundle;
  6. import android.os.PersistableBundle;
  7. import android.support.annotation.Nullable;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.util.Log;
  10. import android.widget.EditText;
  11. import android.widget.ListAdapter;
  12. import android.widget.ListView;
  13. import android.widget.SimpleAdapter;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. import com.android.volley.Request;
  18. import com.android.volley.Response;
  19.  
  20. import org.json.JSONArray;
  21. import org.json.JSONException;
  22. import org.json.JSONObject;
  23.  
  24. import java.util.ArrayList;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27.  
  28. public class Parent_attend extends AppCompatActivity {
  29.  
  30. private String TAG = Parent_attend.class.getSimpleName();
  31. private ProgressDialog pDialog;
  32. private ListView lv;
  33.  
  34. //final TextView username = (TextView)findViewById(R.id.test);
  35. //final TextView password = (TextView)findViewById(R.id.test2);
  36. // URL to get contacts JSON
  37. ArrayList<HashMap<String, String>> contactList;
  38. Intent intent = getIntent();
  39. final String username = intent.getStringExtra("username");
  40. final String password = intent.getStringExtra("password");
  41. String url = "https://lenchan139.org/myWorks/fyp/android/attendDetails.php?username=parentX&password=pw";
  42.  
  43. //final String str = intent.getStringExtra("test");
  44. //final String str2 = intent.getStringExtra("test2");
  45. //username.setText(str);
  46. //password.setText(str2);
  47. @Override
  48. protected void onCreate(Bundle savedInstanceState) {
  49. super.onCreate(savedInstanceState);
  50. setContentView(R.layout.activity_parent_attend);
  51. contactList = new ArrayList<>();
  52. //Intent intent = getIntent();
  53. //final String str = intent.getStringExtra("test");
  54. //final String str2 = intent.getStringExtra("test2");
  55. //username.setText(str);
  56. //password.setText(str2);
  57. lv = (ListView) findViewById(R.id.attenList);
  58. new GetContacts().execute();
  59. }
  60. /**
  61. * Async task class to get json by making HTTP call
  62. */
  63. private class GetContacts extends AsyncTask<Void, Void, Void> {
  64.  
  65. @Override
  66. protected void onPreExecute() {
  67. super.onPreExecute();
  68. // Showing progress dialog
  69. pDialog = new ProgressDialog(Parent_attend.this);
  70. pDialog.setMessage("Please wait...");
  71. pDialog.setCancelable(false);
  72. pDialog.show();
  73.  
  74. }
  75.  
  76. @Override
  77. protected Void doInBackground(Void... arg0) {
  78. HttpHandler sh = new HttpHandler();
  79.  
  80. // Making a request to url and getting response
  81. String jsonStr = sh.makeServiceCall(url);
  82.  
  83. Log.e(TAG, "Response from url: " + jsonStr);
  84.  
  85. if (jsonStr != null) {
  86. try {
  87. JSONObject jsonObj = new JSONObject(jsonStr);
  88.  
  89. // Getting JSON Array node
  90. JSONArray contacts = jsonObj.getJSONArray("studArray");
  91.  
  92. // looping through All Contacts
  93. for (int i = 0; i < contacts.length(); i++) {
  94. JSONObject c = contacts.getJSONObject(i);
  95.  
  96. String studName = c.getString("student_name");
  97. String studClass = c.getString("student_class");
  98. // Phone node is JSON Object
  99. JSONArray date = c.getJSONArray("student_attend");
  100. for (int j = 0; j < date.length(); j++) {
  101. JSONObject jsonObject = date.getJSONObject(j);
  102.  
  103. String dates = jsonObject.getString("attend_date");
  104. // tmp hash map for single contact
  105. HashMap<String, String> contact = new HashMap<>();
  106.  
  107. // adding each child node to HashMap key => value
  108. contact.put("studName", studName);
  109. contact.put("studClass", studClass);
  110. contact.put("attenDate", dates);
  111.  
  112. // adding contact to contact list
  113. contactList.add(contact);
  114. }
  115. }
  116.  
  117. } catch (final JSONException e) {
  118. Log.e(TAG, "Json parsing error: " + e.getMessage());
  119. runOnUiThread(new Runnable() {
  120. @Override
  121. public void run() {
  122. Toast.makeText(getApplicationContext(),
  123. "Json parsing error: " + e.getMessage(),
  124. Toast.LENGTH_LONG)
  125. .show();
  126. }
  127. });
  128.  
  129. }
  130. } else {
  131. Log.e(TAG, "Couldn't get json from server.");
  132. runOnUiThread(new Runnable() {
  133. @Override
  134. public void run() {
  135. Toast.makeText(getApplicationContext(),
  136. "Couldn't get json from server. Check LogCat for possible errors!",
  137. Toast.LENGTH_LONG)
  138. .show();
  139. }
  140. });
  141.  
  142. }
  143.  
  144. return null;
  145. }
  146.  
  147. @Override
  148. protected void onPostExecute(Void result) {
  149. super.onPostExecute(result);
  150. // Dismiss the progress dialog
  151. if (pDialog.isShowing())
  152. pDialog.dismiss();
  153. /**
  154. * Updating parsed JSON data into ListView
  155. * */
  156. ListAdapter adapter = new SimpleAdapter(
  157. Parent_attend.this, contactList,
  158. R.layout.attenlist_item, new String[]{"studName","studClass",
  159. "attenDate"}, new int[]{R.id.studName,
  160. R.id.studClass, R.id.attenDate});
  161.  
  162. lv.setAdapter(adapter);
  163. }
  164.  
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement