Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [
- {
- "_id": "595bdcf32c67a3f9ee6c2a21",
- "dn": "ferwetrert",
- "whenChanged": "20170704065349.0Z",
- "name": "Student",
- "mail": "student@mail.com",
- "updated_at": "2017-07-04T18:22:43.624Z"
- },
- {
- "_id": "595bdcf32c67a3f9ee6c2a25",
- "dn": "CN=Accounting,OU=users,OU=bluERP,OU=companies,DC=blu,DC=local",
- "givenName": "given name",
- "whenChanged": "20170801114732.0Z",
- "name": "Accounting",
- "mail": "accounting@mail.com",
- "updated_at": "2017-07-04T18:22:43.641Z"
- },
- {
- "_id": "590321138",
- "dn": "CN=hallo name,OU=emplyee,OU=Organisation,DC=com,DC=local",
- "sn": "",
- "title": "developer",
- "givenName": "Tina",
- "whenChanged": "20170809073930.0Z",
- "department": "Mobile",
- "company": "Private limited",
- "name": "Full Name",
- "mail": "mail@yahoo.com",
- "mobile": "+123456",
- "updated_at": "2017-04-28T11:01:39.475Z"
- }
- ]
- public class ColleagueModel {
- private String id;
- private String dn;
- private String sn;
- private String givenName;
- private String whenChanged;
- private String name;
- private String mail;
- private String updatedAt;
- private String title;
- private String department;
- private String company;
- private String mobile;
- public ColleagueModel(){
- }
- public ColleagueModel(){
- }
- public ColleagueModel(String name, String company,String title) {
- this.name = name;
- this.company = company;
- this.title = title;
- }
- public class MyColleaguesPage extends AppCompatActivity implements MyColleaguesAdapter.ColleagueListListener {
- // CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds
- public static final int CONNECTION_TIMEOUT = 10000;
- public static final int READ_TIMEOUT = 15000;
- private RecyclerView recyclerView;
- private MyColleaguesAdapter adapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.mycolleagues_layout);
- new AsyncFetch().execute();
- }
- private class AsyncFetch extends AsyncTask<String, String, String>{
- HttpURLConnection conn;
- URL url = null;
- @Override
- protected String doInBackground(String... strings) {
- try {
- // Enter URL address where your json file resides
- // Even you can make call to php file which returns json data
- url = new URL("https://app.../api/users");
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return e.toString();
- }
- try {
- // Setup HttpURLConnection class to send and receive data from php and mysql
- conn = (HttpURLConnection) url.openConnection();
- conn.setReadTimeout(READ_TIMEOUT);
- conn.setConnectTimeout(CONNECTION_TIMEOUT);
- conn.setRequestMethod("GET");
- // setDoOutput to true as we recieve data from json file
- conn.setDoOutput(true);
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- return e1.toString();
- }
- try {
- int response_code = conn.getResponseCode();
- // Check if successful connection made
- if (response_code == HttpURLConnection.HTTP_OK) {
- // Read data sent from server
- InputStream input = conn.getInputStream();
- BufferedReader reader = new BufferedReader(new InputStreamReader(input));
- StringBuilder result = new StringBuilder();
- String line;
- while ((line = reader.readLine()) != null) {
- result.append(line);
- }
- // Pass data to onPostExecute method
- return (result.toString());
- } else {
- return ("unsuccessful");
- }
- } catch (IOException e) {
- e.printStackTrace();
- return e.toString();
- } finally {
- conn.disconnect();
- }
- }
- @Override
- protected void onPostExecute(String result) {
- //this method will be running on UI thread
- List<ColleagueModel> data=new ArrayList<>();
- try {
- JSONArray jArray = new JSONArray(result);
- // Extract data from json and store into ArrayList as class objects
- for(int i=0;i<jArray.length();i++){
- JSONObject json_data = jArray.getJSONObject(i);
- ColleagueModel model = new ColleagueModel();
- String val1=json_data.optString("name");
- String val2= json_data.optString("company");
- String val3=json_data.optString("title");
- model = new ColleagueModel(val1,val2,val3);
- data.add(model);
- }
- // Setup and Handover data to recyclerview
- recyclerView = findViewById(R.id.colleagues_recycler);
- adapter = new MyColleaguesAdapter(MyColleaguesPage.this, data);
- recyclerView.setAdapter(adapter);
- recyclerView.setLayoutManager(new LinearLayoutManager(MyColleaguesPage.this));
- } catch (JSONException e) {
- Toast.makeText(MyColleaguesPage.this, e.toString(), Toast.LENGTH_LONG).show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement