Guest User

Untitled

a guest
Mar 8th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1. // Api.java
  2.  
  3. import java.util.List;
  4.  
  5. import retrofit2.Call;
  6. import retrofit2.http.Body;
  7. import retrofit2.http.GET;
  8. import retrofit2.http.POST;
  9.  
  10. public interface Api {
  11.  
  12. String BASE_URL = "http://10.0.2.2:3000/";
  13.  
  14. @GET("users")
  15. Call<List<User>> getUsers();
  16.  
  17. @POST("users")
  18. Call<User> createUser(@Body User user);
  19.  
  20. }
  21.  
  22.  
  23. // user class
  24.  
  25. public class User {
  26. public String name;
  27. public String email;
  28. public String password;
  29.  
  30. public User(String name, String email, String password) {
  31. this.name = name;
  32. this.email = email;
  33. this.password = password;
  34. }
  35.  
  36. public String getName() {
  37. return name;
  38. }
  39.  
  40. public void setName(String name) {
  41. this.name = name;
  42. }
  43.  
  44. public String getEmail() {
  45. return email;
  46. }
  47.  
  48. public void setEmail(String email) {
  49. this.email = email;
  50. }
  51.  
  52. public String getPassword() {
  53. return password;
  54. }
  55.  
  56. public void setPassword(String password) {
  57. this.password = password;
  58. }
  59.  
  60. }
  61.  
  62.  
  63. // Main Activity .. layout...
  64.  
  65. <?xml version="1.0" encoding="utf-8"?>
  66. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  67. xmlns:app="http://schemas.android.com/apk/res-auto"
  68. xmlns:tools="http://schemas.android.com/tools"
  69. android:layout_width="match_parent"
  70. android:layout_height="match_parent"
  71. android:orientation="vertical">
  72. <TextView
  73. android:layout_width="match_parent"
  74. android:layout_height="wrap_content"
  75. android:text="Enter the user details"
  76. android:textAlignment="center"/>
  77. <LinearLayout
  78. android:layout_width="match_parent"
  79. android:layout_height="wrap_content"
  80. android:weightSum="3">
  81.  
  82. <TextView
  83. android:layout_width="0dp"
  84. android:layout_height="wrap_content"
  85. android:text="Name"
  86. android:textAlignment="center"
  87. android:layout_weight="1"/>
  88. <EditText
  89. android:layout_width="0dp"
  90. android:layout_height="wrap_content"
  91. android:id="@+id/username"
  92. android:layout_weight="2"/>
  93. </LinearLayout>
  94. <LinearLayout
  95. android:layout_width="match_parent"
  96. android:layout_height="wrap_content"
  97. android:weightSum="3">
  98.  
  99. <TextView
  100. android:layout_width="0dp"
  101. android:layout_height="wrap_content"
  102. android:text="Email"
  103. android:textAlignment="center"
  104. android:layout_weight="1"/>
  105. <EditText
  106. android:layout_width="0dp"
  107. android:layout_height="wrap_content"
  108. android:id="@+id/email"
  109. android:layout_weight="2"/>
  110. </LinearLayout>
  111. <LinearLayout
  112. android:layout_width="match_parent"
  113. android:layout_height="wrap_content"
  114. android:weightSum="3">
  115.  
  116. <TextView
  117. android:layout_width="0dp"
  118. android:layout_height="wrap_content"
  119. android:text="Password"
  120. android:textAlignment="center"
  121. android:layout_weight="1"/>
  122. <EditText
  123. android:layout_width="0dp"
  124. android:layout_height="wrap_content"
  125. android:id="@+id/password"
  126. android:layout_weight="2"/>
  127. </LinearLayout>
  128. <LinearLayout
  129. android:layout_width="match_parent"
  130. android:layout_height="wrap_content"
  131. android:weightSum="2">
  132.  
  133. <Button
  134. android:layout_width="0dp"
  135. android:layout_height="wrap_content"
  136. android:text="Submit"
  137. android:layout_weight="1"
  138. android:id="@+id/submit"/>
  139. <Button
  140. android:layout_width="0dp"
  141. android:layout_height="wrap_content"
  142. android:text="Retrieve"
  143. android:layout_weight="1"
  144. android:id="@+id/retrieve"/>
  145.  
  146. </LinearLayout>
  147. <ListView
  148. android:id="@+id/listViewUsers"
  149. android:layout_width="match_parent"
  150. android:layout_height="wrap_content" />
  151.  
  152. </LinearLayout>
  153.  
  154.  
  155.  
  156. /// Main Java class
  157.  
  158.  
  159. package giri.amity.noidaworkshop;
  160. import android.support.v7.app.AppCompatActivity;
  161. import android.os.Bundle;
  162. import android.util.Log;
  163. import android.view.View;
  164. import android.widget.ArrayAdapter;
  165. import android.widget.Button;
  166. import android.widget.EditText;
  167. import android.widget.ListView;
  168. import android.widget.Toast;
  169.  
  170. import java.util.List;
  171.  
  172. import retrofit2.Call;
  173. import retrofit2.Callback;
  174. import retrofit2.Response;
  175. import retrofit2.Retrofit;
  176. import retrofit2.converter.gson.GsonConverterFactory;
  177.  
  178. public class MainActivity extends AppCompatActivity {
  179. ListView listView;
  180. @Override
  181. protected void onCreate(Bundle savedInstanceState) {
  182. super.onCreate(savedInstanceState);
  183. setContentView(R.layout.activity_main);
  184.  
  185. Button submit = (Button)findViewById(R.id.submit);
  186. submit.setOnClickListener(new View.OnClickListener() {
  187. @Override
  188. public void onClick(View v) {
  189. final EditText name = (EditText)findViewById(R.id.username);
  190. final EditText email = (EditText)findViewById(R.id.email);
  191. final EditText password = (EditText)findViewById(R.id.password);
  192. User user = new User(name.getText().toString(),email.getText().toString(),password.getText().toString());
  193. Retrofit retrofit = new Retrofit.Builder()
  194. .baseUrl(Api.BASE_URL)
  195. .addConverterFactory(GsonConverterFactory.create())
  196. .build();
  197.  
  198.  
  199. Api api = retrofit.create(Api.class);
  200. Call<List<User>> call = api.getUsers();
  201. Api api1 = retrofit.create(Api.class);
  202. Call<User> call1 = api1.createUser(user);
  203. call1.enqueue(new Callback<User>() {
  204. @Override
  205. public void onResponse(Call<User> call, Response<User> response) {
  206. Toast.makeText(getApplicationContext(), response.message(), Toast.LENGTH_LONG).show();
  207. name.setText("");
  208. email.setText("");
  209. password.setText("");
  210.  
  211.  
  212. }
  213.  
  214. @Override
  215. public void onFailure(Call<User> call, Throwable t) {
  216.  
  217.  
  218.  
  219. }
  220. });
  221. }
  222. });
  223.  
  224. Button retrieve = (Button)findViewById(R.id.retrieve);
  225. retrieve.setOnClickListener(new View.OnClickListener() {
  226. @Override
  227. public void onClick(View v) {
  228. getUsers();
  229.  
  230. }
  231. });
  232.  
  233. listView = (ListView) findViewById(R.id.listViewUsers);
  234.  
  235. //calling the method to display the heroes
  236.  
  237.  
  238.  
  239. }
  240. public void getUsers(){
  241.  
  242.  
  243. Retrofit retrofit = new Retrofit.Builder()
  244. .baseUrl(Api.BASE_URL)
  245. .addConverterFactory(GsonConverterFactory.create())
  246. .build();
  247.  
  248.  
  249. Api api = retrofit.create(Api.class);
  250. Call<List<User>> call = api.getUsers();
  251.  
  252. call.enqueue(new Callback<List<User>>() {
  253. @Override
  254. public void onResponse(Call<List<User>> call, Response<List<User>> response) {
  255. List<User> usersList = response.body();
  256. String[] users = new String[usersList.size()];
  257.  
  258. //looping through all the heroes and inserting the names inside the string array
  259. for (int i = 0; i < usersList.size(); i++) {
  260. users[i] = usersList.get(i).getName();
  261. }
  262.  
  263.  
  264. //displaying the string array into listview
  265. listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, users));
  266.  
  267. }
  268.  
  269. @Override
  270. public void onFailure(Call<List<User>> call, Throwable t) {
  271. Log.d("hello", t.toString());
  272. Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
  273.  
  274. }
  275. });
  276.  
  277. }
  278. }
  279.  
  280.  
  281.  
  282.  
  283. // retrofit ... dependency
  284.  
  285.  
  286. implementation 'com.squareup.retrofit2:retrofit:2.5.0'
  287. implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
  288.  
  289.  
  290. // internet permission and application cleartexttraffic
  291.  
  292. android:usesCleartextTraffic="true"
Add Comment
Please, Sign In to add comment