private void PreviousLoginActivity() { //Send the login username and password to the server and get response string apiUrl = "https://ycandgap.me/api_server2.php"; string apiMethod = "loginUser"; //Login_Request has two properties:username and password Login_Request myLogin_Request = new Login_Request(); myLogin_Request.Username = username.Text; myLogin_Request.Password = password.Text.GetHashCode(); // make http post request string response = Http.Post(apiUrl, new NameValueCollection() { { "api_method", apiMethod }, { "api_data", JsonConvert.SerializeObject(myLogin_Request) } }); // decode json string to dto object API_Response r = JsonConvert.DeserializeObject<API_Response>(response); // check response if (!r.IsError && r.ResponseData != null) { //**if login successfully, go to FriendsList page with the username ISharedPreferences sharedPref = PreferenceManager.GetDefaultSharedPreferences(this); ISharedPreferencesEditor editor = sharedPref.Edit(); editor.PutString("RegistrationId", r.ResponseData); editor.Apply(); var friendsActivity = new Intent(this, typeof(FriendsActivity)); friendsActivity.PutExtra("UserRegisterID", r.ResponseData); StartActivity(typeof(FriendsActivity)); } else { //if login fails, pop up an alert message. Wrong username or password or a new user AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); dialogBuilder.SetMessage(r.ErrorMessage); //dialogBuilder.SetPositiveButton("Ok", null); dialogBuilder.Show(); } }
private void RequestRegister(uint registrationId, IdentityKeyPair identityKeyPair, SignedPreKeyRecord signedPreKey, List <PreKeyRecord> preKeys) { //Send the login username and password to the server and get response string apiUrl = "https://ycandgap.me/api_server2.php"; string apiMethod = "registerUser"; // Login_Request has two properties: username and password Login_Request myLogin_Request = new Login_Request(); myLogin_Request.Username = username.Text; myLogin_Request.Password = password.Text.GetHashCode(); myLogin_Request.RegistrationID = registrationId; myLogin_Request.PublicIdentityKey = JsonConvert.SerializeObject(identityKeyPair.getPublicKey().serialize()); myLogin_Request.PublicSignedPreKeyID = signedPreKey.getId(); myLogin_Request.PublicSignedPreKeySignature = JsonConvert.SerializeObject(signedPreKey.getSignature()); myLogin_Request.PublicSignedPreKey = JsonConvert.SerializeObject(signedPreKey.getKeyPair().getPublicKey().serialize()); // Save in local Database ISharedPreferences sharedprefs = PreferenceManager.GetDefaultSharedPreferences(this); ISharedPreferencesEditor editor = sharedprefs.Edit(); // Store identityKeyPair somewhere durable and safe. editor.PutString("IdentityKeyPair", JsonConvert.SerializeObject(identityKeyPair.serialize())); editor.PutString("SignedPreKey", JsonConvert.SerializeObject(signedPreKey.serialize())); editor.PutString("Username", username.Text); editor.PutInt("Password", password.Text.GetHashCode()); // Store registrationId somewhere durable and safe. editor.PutString("RegistrationId", registrationId.ToString()); editor.Apply(); // make http post request string response = Http.Post(apiUrl, new NameValueCollection() { { "api_method", apiMethod }, { "api_data", JsonConvert.SerializeObject(myLogin_Request) } }); // decode json string to dto object API_Response r = JsonConvert.DeserializeObject <API_Response>(response); if (r != null) { if (!r.IsError) { foreach (PreKeyRecord preKey in preKeys) { Prekey_Request preKey_Request = new Prekey_Request(); preKey_Request.PublicSignedPreKeyID = signedPreKey.getId(); preKey_Request.PublicPreKeyID = preKey.getId(); preKey_Request.PublicPreKey = JsonConvert.SerializeObject(preKey.getKeyPair().getPublicKey().serialize()); apiMethod = "storePreKeys"; // make http post request string preKeyResponse = Http.Post(apiUrl, new NameValueCollection() { { "api_method", apiMethod }, { "api_data", JsonConvert.SerializeObject(preKey_Request) } }); // decode json string to dto object API_Response preKeyR = JsonConvert.DeserializeObject <API_Response>(preKeyResponse); if (preKeyR == null) { break; } } } } }