Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. [
  2. {
  3. "_id": "595bdcf32c67a3f9ee6c2a21",
  4. "dn": "ferwetrert",
  5. "whenChanged": "20170704065349.0Z",
  6. "name": "Student",
  7. "mail": "student@mail.com",
  8. "updated_at": "2017-07-04T18:22:43.624Z"
  9. },
  10. {
  11. "_id": "595bdcf32c67a3f9ee6c2a25",
  12. "dn": "CN=Accounting,OU=users,OU=bluERP,OU=companies,DC=blu,DC=local",
  13. "givenName": "given name",
  14. "whenChanged": "20170801114732.0Z",
  15. "name": "Accounting",
  16. "mail": "accounting@mail.com",
  17. "updated_at": "2017-07-04T18:22:43.641Z"
  18. },
  19. {
  20. "_id": "590321138",
  21. "dn": "CN=hallo name,OU=emplyee,OU=Organisation,DC=com,DC=local",
  22. "sn": "",
  23. "title": "developer",
  24. "givenName": "Tina",
  25. "whenChanged": "20170809073930.0Z",
  26. "department": "Mobile",
  27. "company": "Private limited",
  28. "name": "Full Name",
  29. "mail": "mail@yahoo.com",
  30. "mobile": "+123456",
  31. "updated_at": "2017-04-28T11:01:39.475Z"
  32. }
  33. ]
  34.  
  35. public class ColleagueModel {
  36.  
  37.  
  38. private String id;
  39. private String dn;
  40. private String sn;
  41. private String givenName;
  42. private String whenChanged;
  43. private String name;
  44. private String mail;
  45. private String updatedAt;
  46. private String title;
  47. private String department;
  48. private String company;
  49. private String mobile;
  50.  
  51. public ColleagueModel(){
  52.  
  53. }
  54. public ColleagueModel(){
  55.  
  56. }
  57.  
  58. public ColleagueModel(String name, String company,String title) {
  59. this.name = name;
  60. this.company = company;
  61. this.title = title;
  62.  
  63. }
  64.  
  65. public class MyColleaguesPage extends AppCompatActivity implements MyColleaguesAdapter.ColleagueListListener {
  66.  
  67. // CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds
  68. public static final int CONNECTION_TIMEOUT = 10000;
  69. public static final int READ_TIMEOUT = 15000;
  70.  
  71. private RecyclerView recyclerView;
  72. private MyColleaguesAdapter adapter;
  73.  
  74.  
  75. @Override
  76. protected void onCreate(Bundle savedInstanceState) {
  77. super.onCreate(savedInstanceState);
  78. setContentView(R.layout.mycolleagues_layout);
  79.  
  80. new AsyncFetch().execute();
  81.  
  82. }
  83.  
  84. private class AsyncFetch extends AsyncTask<String, String, String>{
  85. HttpURLConnection conn;
  86. URL url = null;
  87. @Override
  88. protected String doInBackground(String... strings) {
  89. try {
  90.  
  91. // Enter URL address where your json file resides
  92. // Even you can make call to php file which returns json data
  93. url = new URL("https://app.../api/users");
  94.  
  95. } catch (MalformedURLException e) {
  96. // TODO Auto-generated catch block
  97. e.printStackTrace();
  98. return e.toString();
  99. }
  100. try {
  101.  
  102. // Setup HttpURLConnection class to send and receive data from php and mysql
  103. conn = (HttpURLConnection) url.openConnection();
  104. conn.setReadTimeout(READ_TIMEOUT);
  105. conn.setConnectTimeout(CONNECTION_TIMEOUT);
  106. conn.setRequestMethod("GET");
  107.  
  108. // setDoOutput to true as we recieve data from json file
  109. conn.setDoOutput(true);
  110.  
  111. } catch (IOException e1) {
  112. // TODO Auto-generated catch block
  113. e1.printStackTrace();
  114. return e1.toString();
  115. }
  116.  
  117. try {
  118.  
  119. int response_code = conn.getResponseCode();
  120.  
  121. // Check if successful connection made
  122. if (response_code == HttpURLConnection.HTTP_OK) {
  123.  
  124. // Read data sent from server
  125. InputStream input = conn.getInputStream();
  126. BufferedReader reader = new BufferedReader(new InputStreamReader(input));
  127. StringBuilder result = new StringBuilder();
  128. String line;
  129.  
  130. while ((line = reader.readLine()) != null) {
  131. result.append(line);
  132. }
  133.  
  134. // Pass data to onPostExecute method
  135. return (result.toString());
  136.  
  137. } else {
  138.  
  139. return ("unsuccessful");
  140. }
  141.  
  142. } catch (IOException e) {
  143. e.printStackTrace();
  144. return e.toString();
  145. } finally {
  146. conn.disconnect();
  147. }
  148.  
  149. }
  150.  
  151. @Override
  152. protected void onPostExecute(String result) {
  153.  
  154. //this method will be running on UI thread
  155.  
  156.  
  157. List<ColleagueModel> data=new ArrayList<>();
  158.  
  159. try {
  160.  
  161.  
  162. JSONArray jArray = new JSONArray(result);
  163.  
  164.  
  165. // Extract data from json and store into ArrayList as class objects
  166. for(int i=0;i<jArray.length();i++){
  167. JSONObject json_data = jArray.getJSONObject(i);
  168. ColleagueModel model = new ColleagueModel();
  169. String val1=json_data.optString("name");
  170. String val2= json_data.optString("company");
  171. String val3=json_data.optString("title");
  172. model = new ColleagueModel(val1,val2,val3);
  173. data.add(model);
  174. }
  175.  
  176. // Setup and Handover data to recyclerview
  177. recyclerView = findViewById(R.id.colleagues_recycler);
  178. adapter = new MyColleaguesAdapter(MyColleaguesPage.this, data);
  179. recyclerView.setAdapter(adapter);
  180. recyclerView.setLayoutManager(new LinearLayoutManager(MyColleaguesPage.this));
  181.  
  182. } catch (JSONException e) {
  183. Toast.makeText(MyColleaguesPage.this, e.toString(), Toast.LENGTH_LONG).show();
  184. }
  185.  
  186. }
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement