Guest User

Untitled

a guest
Sep 3rd, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. {
  2. "success": true,
  3. "message": "",
  4. "user": [
  5. {
  6. "id": 1,
  7. "username": "Admin",
  8. "email": "admin@gmail.com",
  9. "role": "Administrator"
  10. },
  11. {
  12. "id": 2,
  13. "username": "Lawyer",
  14. "email": "lawyer@gmail.com",
  15. "role": "Lawyer"
  16. },
  17. {
  18. "id": 3,
  19. "username": "Driver",
  20. "email": "driver@gmail.com",
  21. "role": "Driver"
  22. }
  23. ]
  24. }
  25.  
  26. <ListView
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:id="@+id/ls"/>
  30.  
  31. public interface api {
  32. String BASE_URL = "https://efce0212.ngrok.io/api/";
  33. @GET("user")
  34. Call<List<User>> getHeroes();
  35. }
  36.  
  37. public class MainActivity extends AppCompatActivity {
  38.  
  39. ListView listView;
  40.  
  41. @Override
  42. protected void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. setContentView(R.layout.activity_main);
  45. listView = findViewById(R.id.ls);
  46. getHeroes();
  47. }
  48.  
  49. public void getHeroes() {
  50. Retrofit retrofit = new Retrofit.Builder()
  51. .baseUrl(api.BASE_URL)
  52. .addConverterFactory(GsonConverterFactory.create())
  53. .build();
  54.  
  55. api api = retrofit.create(api.class);
  56.  
  57.  
  58. Call<List<User>> call = api.getHeroes();
  59.  
  60.  
  61. call.enqueue(new Callback<List<User>>() {
  62. @Override
  63. public void onResponse(Call<List<User>> call, Response<List<User>> response) {
  64. final List<User> userList = response.body();
  65.  
  66. final String[] user = new String[userList.size()];
  67.  
  68. for (int i = 0; i < userList.size(); i++) {
  69.  
  70.  
  71. user[i] = "Sucess:" + userList.get(i).getSucess() + "n" + "message" + userList.get(i).getMessage() + "n" + userList.get(i).getUserData();
  72. }
  73.  
  74. listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, user));
  75. }
  76.  
  77. @Override
  78. public void onFailure(Call<List<User>> call, Throwable t) {
  79.  
  80. Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
  81.  
  82. }
  83. });
  84. }
  85. }
  86.  
  87. public class User {
  88. private String sucess;
  89. private String message;
  90. public UserData[] userData;
  91.  
  92. //UserData userData = new UserData();
  93.  
  94. public User(String sucess, String message, UserData[] userData) {
  95. this.sucess = sucess;
  96. this.message = message;
  97. //this.userData = userData;
  98. this.userData = userData;
  99. }
  100.  
  101. public String getSucess() {
  102. return sucess;
  103. }
  104.  
  105. public String getMessage() {
  106. return message;
  107. }
  108.  
  109. public UserData[] getUserData() {
  110. return userData;
  111. }
  112. }
  113.  
  114. public class UserData {
  115. public String id;
  116. public String name;
  117. public String email;
  118. public String role;
  119. }
  120.  
  121. LinearLayoutManager layoutManager=new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
  122. listView.setLayoutManager(layoutManager);
Add Comment
Please, Sign In to add comment