public User setUser(string firstName, string lastName, string cpf, string password) { User user = new User(); user.firstName = firstName; user.lastName = lastName; user.cpf = cpf; user.password = password; return user; }
protected override void OnCreate(Bundle bundle) { base.OnCreate (bundle); SetContentView (Resource.Layout.Main); TextView firstName = FindViewById<TextView> (Resource.Id.firstNameField); TextView lastName = FindViewById<TextView> (Resource.Id.lastNameField); TextView cpf = FindViewById<TextView> (Resource.Id.cpfField); TextView password = FindViewById<TextView> (Resource.Id.passwordField); Button register = FindViewById<Button> (Resource.Id.RegisterButton); register.Click += (object sender, EventArgs e) => { var confirmDialog = new AlertDialog.Builder (this); confirmDialog.SetMessage ("Cadastrar o usuário: " + firstName.Text + "? Password " + password.Text); confirmDialog.SetNeutralButton ("Sim", delegate { User user = new User(); user = setUser(firstName.Text , lastName.Text , cpf.Text, password.Text); if(UserRegister.userRegister(user)) { Toast.MakeText(this, "Usuário cadastrado com sucesso", ToastLength.Long).Show(); }else{ Toast.MakeText(this, "Erro ao cadastrar usuário", ToastLength.Long).Show(); } }); confirmDialog.SetNegativeButton ("Não", delegate { }); confirmDialog.Show(); }; // Set our view from the "main" layout resource // Get our button from the layout resource, // and attach an event to it }
public static bool userRegister(User user) { String url = "http://10.10.15.41:8080/Futebol/rest/user/register"; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create (new Uri (url)); request.ContentType = "application/json"; request.Method = "POST"; /* Monta o objeto json */ using(var streamWriter = new StreamWriter(request.GetRequestStream())) { JsonObject json = new JsonObject(); json.Add("firstName", user.firstName); json.Add("lastName", user.lastName); json.Add("cpf", user.cpf); json.Add("password", user.password); streamWriter.Write(json.ToString()); streamWriter.Flush(); streamWriter.Close(); } /* Chama o serviço REST */ try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if(response.StatusCode.Equals(HttpStatusCode.OK)){ return true; }else{ return false; } } catch (WebException e) { Console.Write("GUILHERME ERROR: ", e); JsonValue error = null; return error; } }