Created
March 13, 2019 18:49
-
-
Save cod3smith/d014a81c79b34c5810df8af4bcbd0c4c to your computer and use it in GitHub Desktop.
User Object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.musicalcoder.inspireme.network; | |
| import com.musicalcoder.inspireme.model.User; | |
| import com.musicalcoder.inspireme.model.UserResponse; | |
| import retrofit2.Call; | |
| import retrofit2.http.Body; | |
| import retrofit2.http.POST; | |
| public interface Service { | |
| @POST("auth/register") | |
| Call<User> createAccount(@Body User user); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.musicalcoder.inspireme.model; | |
| public class User { | |
| private Integer id; | |
| private String fullname; | |
| private String email; | |
| private String password1; | |
| private String password2; | |
| public User(String fullname, String email, String password1, String password2) { | |
| this.fullname = fullname; | |
| this.email = email; | |
| this.password1 = password1; | |
| this.password2 = password2; | |
| } | |
| public Integer getId() { | |
| return id; | |
| } | |
| public String getFullname() { | |
| return fullname; | |
| } | |
| public String getEmail() { | |
| return email; | |
| } | |
| public String getPassword1() { | |
| return password1; | |
| } | |
| public String getPassword2() { | |
| return password2; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.musicalcoder.inspireme; | |
| import android.os.Bundle; | |
| import android.util.Log; | |
| import android.view.View; | |
| import android.widget.EditText; | |
| import android.widget.Toast; | |
| import com.musicalcoder.inspireme.model.User; | |
| import com.musicalcoder.inspireme.model.UserResponse; | |
| import com.musicalcoder.inspireme.network.InspireClient; | |
| import com.musicalcoder.inspireme.network.Service; | |
| import androidx.appcompat.app.AppCompatActivity; | |
| import butterknife.BindView; | |
| import butterknife.ButterKnife; | |
| import butterknife.OnClick; | |
| import retrofit2.Call; | |
| import retrofit2.Callback; | |
| import retrofit2.Response; | |
| public class UserRegistration extends AppCompatActivity { | |
| Service service; | |
| @BindView(R.id.fullname_txt) EditText name; | |
| @BindView(R.id.email_txt) EditText email; | |
| @BindView(R.id.password) EditText password; | |
| @BindView(R.id.confirm_password) EditText confirmPassword; | |
| @OnClick(R.id.create_account) | |
| public void submitData(View view) { | |
| registerUser(); | |
| } | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_user_registration); | |
| ButterKnife.bind(this); | |
| } | |
| public void registerUser() { | |
| String fullName = name.getText().toString(); | |
| String emailText = email.getText().toString(); | |
| String enteredPassword = password.getText().toString(); | |
| String confirmation = confirmPassword.getText().toString(); | |
| service = InspireClient.fetchClient().create(Service.class); | |
| User user = new User(fullName, emailText, enteredPassword, confirmation); | |
| Call<User> userCall = service.createAccount(user); | |
| userCall.enqueue(new Callback<User>() { | |
| @Override | |
| public void onResponse(Call<User> call, Response<User> response) { | |
| if(response.isSuccessful()){ | |
| User newUser = response.body(); | |
| Log.d("Message", "User created!"); | |
| Toast.makeText(getApplicationContext(), "User "+ newUser.getId() + "Created" , Toast.LENGTH_SHORT).show(); | |
| } else { | |
| Log.d("Code", String.valueOf(response.code())); | |
| Log.d("Response Body", String.valueOf(response.body())); | |
| } | |
| } | |
| @Override | |
| public void onFailure(Call<User> call, Throwable t) { | |
| call.cancel(); | |
| } | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment