Guest User

Untitled

a guest
Feb 2nd, 2018
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. public interface APIService {
  2.  
  3.  
  4. @Headers({"Content-Type: application/json",
  5. "Authorization: Basic Z2VldGFuamFsaTpnZWV0YW5qYWxp"})
  6. @POST("account/register")
  7. Call<ResponseBody> createUser(@Body User user);
  8.  
  9. public class User {
  10. private String email;
  11. private String password;
  12.  
  13. public User(String email, String password) {
  14. this.email = email;
  15. this.password = password;
  16. }
  17.  
  18. public String getEmail() {
  19. return email;
  20. }
  21.  
  22. public String getPassword() {
  23. return password;
  24. }
  25.  
  26. private void userSignUp() {
  27. //defining a progress dialog to show while signing up
  28. final ProgressDialog progressDialog = new ProgressDialog(this);
  29. progressDialog.setMessage("Signing Up...");
  30. progressDialog.show();
  31.  
  32. String email = signUpEmail.getText().toString().trim();
  33. String password = signUpPassword.getText().toString().trim();
  34.  
  35. //building retrofit object
  36. Retrofit retrofit = new Retrofit.Builder()
  37. .baseUrl(APIUrl.BASE_URL)
  38. .addConverterFactory(GsonConverterFactory.create())
  39. .build();
  40.  
  41. //Defining retrofit api service
  42. APIService service = retrofit.create(APIService.class);
  43.  
  44. //Defining the user object as we need to pass it with the call
  45. User user = new User(email, password);
  46.  
  47. //defining the call
  48. Call<ResponseBody> call = service.createUser(user);
  49.  
  50. //calling the api
  51. call.enqueue(new Callback<ResponseBody>() {
  52. @Override
  53. public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
  54. //hiding progress dialog
  55. progressDialog.dismiss();
  56.  
  57. try {
  58. responseMessage = response.body().string();
  59. } catch (IOException e) {
  60. e.printStackTrace();
  61. }
  62. //displaying the message from the response as toast
  63. Toast.makeText(getApplicationContext(), responseMessage, Toast.LENGTH_LONG).show();
  64. }
  65.  
  66. @Override
  67. public void onFailure(Call<ResponseBody> call, Throwable t) {
  68. progressDialog.dismiss();
  69. Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
  70. }
  71. });
  72. }
Add Comment
Please, Sign In to add comment