Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Api.java
- import java.util.List;
- import retrofit2.Call;
- import retrofit2.http.Body;
- import retrofit2.http.GET;
- import retrofit2.http.POST;
- public interface Api {
- String BASE_URL = "http://10.0.2.2:3000/";
- @GET("users")
- Call<List<User>> getUsers();
- @POST("users")
- Call<User> createUser(@Body User user);
- }
- // user class
- public class User {
- public String name;
- public String email;
- public String password;
- public User(String name, String email, String password) {
- this.name = name;
- this.email = email;
- this.password = password;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
- // Main Activity .. layout...
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Enter the user details"
- android:textAlignment="center"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:weightSum="3">
- <TextView
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:text="Name"
- android:textAlignment="center"
- android:layout_weight="1"/>
- <EditText
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:id="@+id/username"
- android:layout_weight="2"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:weightSum="3">
- <TextView
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:text="Email"
- android:textAlignment="center"
- android:layout_weight="1"/>
- <EditText
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:id="@+id/email"
- android:layout_weight="2"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:weightSum="3">
- <TextView
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:text="Password"
- android:textAlignment="center"
- android:layout_weight="1"/>
- <EditText
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:id="@+id/password"
- android:layout_weight="2"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:weightSum="2">
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:text="Submit"
- android:layout_weight="1"
- android:id="@+id/submit"/>
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:text="Retrieve"
- android:layout_weight="1"
- android:id="@+id/retrieve"/>
- </LinearLayout>
- <ListView
- android:id="@+id/listViewUsers"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
- /// Main Java class
- package giri.amity.noidaworkshop;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.Toast;
- import java.util.List;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- import retrofit2.Retrofit;
- import retrofit2.converter.gson.GsonConverterFactory;
- public class MainActivity extends AppCompatActivity {
- ListView listView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button submit = (Button)findViewById(R.id.submit);
- submit.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- final EditText name = (EditText)findViewById(R.id.username);
- final EditText email = (EditText)findViewById(R.id.email);
- final EditText password = (EditText)findViewById(R.id.password);
- User user = new User(name.getText().toString(),email.getText().toString(),password.getText().toString());
- Retrofit retrofit = new Retrofit.Builder()
- .baseUrl(Api.BASE_URL)
- .addConverterFactory(GsonConverterFactory.create())
- .build();
- Api api = retrofit.create(Api.class);
- Call<List<User>> call = api.getUsers();
- Api api1 = retrofit.create(Api.class);
- Call<User> call1 = api1.createUser(user);
- call1.enqueue(new Callback<User>() {
- @Override
- public void onResponse(Call<User> call, Response<User> response) {
- Toast.makeText(getApplicationContext(), response.message(), Toast.LENGTH_LONG).show();
- name.setText("");
- email.setText("");
- password.setText("");
- }
- @Override
- public void onFailure(Call<User> call, Throwable t) {
- }
- });
- }
- });
- Button retrieve = (Button)findViewById(R.id.retrieve);
- retrieve.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- getUsers();
- }
- });
- listView = (ListView) findViewById(R.id.listViewUsers);
- //calling the method to display the heroes
- }
- public void getUsers(){
- Retrofit retrofit = new Retrofit.Builder()
- .baseUrl(Api.BASE_URL)
- .addConverterFactory(GsonConverterFactory.create())
- .build();
- Api api = retrofit.create(Api.class);
- Call<List<User>> call = api.getUsers();
- call.enqueue(new Callback<List<User>>() {
- @Override
- public void onResponse(Call<List<User>> call, Response<List<User>> response) {
- List<User> usersList = response.body();
- String[] users = new String[usersList.size()];
- //looping through all the heroes and inserting the names inside the string array
- for (int i = 0; i < usersList.size(); i++) {
- users[i] = usersList.get(i).getName();
- }
- //displaying the string array into listview
- listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, users));
- }
- @Override
- public void onFailure(Call<List<User>> call, Throwable t) {
- Log.d("hello", t.toString());
- Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
- }
- });
- }
- }
- // retrofit ... dependency
- implementation 'com.squareup.retrofit2:retrofit:2.5.0'
- implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
- // internet permission and application cleartexttraffic
- android:usesCleartextTraffic="true"
Add Comment
Please, Sign In to add comment