api rest
This commit is contained in:
parent
acf56124ab
commit
9d78ad4531
3 changed files with 183 additions and 17 deletions
|
@ -3,6 +3,8 @@
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
package="asgardius.page.r3forumtest">
|
package="asgardius.page.r3forumtest">
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||||
|
|
|
@ -6,14 +6,31 @@ import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
|
import android.widget.EditText;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.net.ssl.HttpsURLConnection;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
Button signUp, login;
|
||||||
|
EditText user, pwd;
|
||||||
|
URL endpoint;
|
||||||
|
HttpsURLConnection myConnection;
|
||||||
|
boolean success;
|
||||||
|
String myData;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
Button signUp = (Button)findViewById(R.id.signup);
|
user = (EditText)findViewById(R.id.username);
|
||||||
|
pwd = (EditText)findViewById(R.id.password);
|
||||||
|
signUp = (Button)findViewById(R.id.signup);
|
||||||
|
login = (Button)findViewById(R.id.login);
|
||||||
signUp.setOnClickListener(new View.OnClickListener(){
|
signUp.setOnClickListener(new View.OnClickListener(){
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
|
@ -21,6 +38,71 @@ public class MainActivity extends AppCompatActivity {
|
||||||
SignUpForm();
|
SignUpForm();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
login.setOnClickListener(new View.OnClickListener(){
|
||||||
|
@Override
|
||||||
|
public void onClick(View view) {
|
||||||
|
//buttonaction
|
||||||
|
if(user.getText().toString().equals("") || pwd.getText().toString().equals("")) {
|
||||||
|
Toast.makeText(getApplicationContext(),"Introduzac el usuario y contraseña", Toast.LENGTH_SHORT).show();
|
||||||
|
} else {
|
||||||
|
Thread login = new Thread(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
//Your code goes here
|
||||||
|
endpoint = new URL("https://desktop.asgardius.company/test/restful/items/check.php");
|
||||||
|
myConnection = (HttpsURLConnection) endpoint.openConnection();
|
||||||
|
myConnection.setRequestProperty("User-Agent", "r3-forum-test");
|
||||||
|
myConnection.setRequestMethod("POST");
|
||||||
|
// Create the data
|
||||||
|
myData = "{\n" +
|
||||||
|
"\"id\": \""+user.getText().toString()+"\",\n" +
|
||||||
|
"\"password\": \""+pwd.getText().toString()+"\"\n" +
|
||||||
|
"}";
|
||||||
|
// Enable writing
|
||||||
|
myConnection.setDoOutput(true);
|
||||||
|
// Write the data
|
||||||
|
myConnection.getOutputStream().write(myData.getBytes());
|
||||||
|
System.out.println(myConnection.getResponseCode());
|
||||||
|
if (myConnection.getResponseCode() == 201) {
|
||||||
|
success = true;
|
||||||
|
} else {
|
||||||
|
success = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
//Test
|
||||||
|
if (success) {
|
||||||
|
Toast.makeText(getApplicationContext(), "Credenciales correctas", Toast.LENGTH_SHORT).show();
|
||||||
|
} else {
|
||||||
|
Toast.makeText(getApplicationContext(), "Credenciales incorrectas", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Toast.makeText(getApplicationContext(), "Conexión fallida", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
//Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show();
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
login.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SignUpForm () {
|
private void SignUpForm () {
|
||||||
|
|
|
@ -1,35 +1,117 @@
|
||||||
package asgardius.page.r3forumtest;
|
package asgardius.page.r3forumtest;
|
||||||
|
|
||||||
import android.app.DatePickerDialog;
|
|
||||||
import android.app.Dialog;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
|
||||||
import com.google.android.material.appbar.CollapsingToolbarLayout;
|
|
||||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
|
||||||
import com.google.android.material.snackbar.Snackbar;
|
|
||||||
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.appcompat.widget.Toolbar;
|
|
||||||
|
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.DatePicker;
|
import android.widget.EditText;
|
||||||
import android.widget.TextView;
|
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import java.util.Calendar;
|
import java.net.URL;
|
||||||
|
|
||||||
import asgardius.page.r3forumtest.databinding.ActivitySignUpBinding;
|
import javax.net.ssl.HttpsURLConnection;
|
||||||
|
|
||||||
public class SignUp extends AppCompatActivity {
|
public class SignUp extends AppCompatActivity {
|
||||||
private DatePicker datePicker;
|
Button signUp;
|
||||||
private Calendar calendar;
|
EditText user, pwd, pwdc, nacion, fname, lname, mail, dated, datem, datey;
|
||||||
private Button dateGet;
|
URL endpoint;
|
||||||
private int year, month, day;
|
HttpsURLConnection myConnection;
|
||||||
private TextView dateView;
|
boolean success;
|
||||||
|
String myData;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_sign_up);
|
setContentView(R.layout.activity_sign_up);
|
||||||
|
user = (EditText)findViewById(R.id.username);
|
||||||
|
pwd = (EditText)findViewById(R.id.password);
|
||||||
|
pwdc = (EditText)findViewById(R.id.passwordConfirm);
|
||||||
|
nacion = (EditText)findViewById(R.id.nacionalidad);
|
||||||
|
fname = (EditText)findViewById(R.id.firstname);
|
||||||
|
mail = (EditText)findViewById(R.id.email);
|
||||||
|
dated = (EditText)findViewById(R.id.Date);
|
||||||
|
datem = (EditText)findViewById(R.id.Month);
|
||||||
|
datey = (EditText)findViewById(R.id.Year);
|
||||||
|
lname = (EditText)findViewById(R.id.lastname);
|
||||||
|
signUp = (Button)findViewById(R.id.signup);
|
||||||
|
|
||||||
|
signUp.setOnClickListener(new View.OnClickListener(){
|
||||||
|
@Override
|
||||||
|
public void onClick(View view) {
|
||||||
|
//buttonaction
|
||||||
|
if(user.getText().toString().equals("") || pwd.getText().toString().equals("") ||
|
||||||
|
pwdc.getText().toString().equals("") || nacion.getText().toString().equals("") ||
|
||||||
|
fname.getText().toString().equals("") || lname.getText().toString().equals("") ||
|
||||||
|
mail.getText().toString().equals("") || dated.getText().toString().equals("") ||
|
||||||
|
datem.getText().toString().equals("") || datey.getText().toString().equals("")) {
|
||||||
|
Toast.makeText(getApplicationContext(),"Todos los campos son obligatorios", Toast.LENGTH_SHORT).show();
|
||||||
|
} else if(!pwd.getText().toString().equals(pwdc.getText().toString())) {
|
||||||
|
Toast.makeText(getApplicationContext(),"Las contraseñas no coinciden", Toast.LENGTH_SHORT).show();
|
||||||
|
}else {
|
||||||
|
Thread login = new Thread(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
//Your code goes here
|
||||||
|
endpoint = new URL("https://desktop.asgardius.company/test/restful/items/create.php");
|
||||||
|
myConnection = (HttpsURLConnection) endpoint.openConnection();
|
||||||
|
myConnection.setRequestProperty("User-Agent", "r3-forum-test");
|
||||||
|
myConnection.setRequestMethod("POST");
|
||||||
|
// Create the data
|
||||||
|
myData = "{\n" +
|
||||||
|
"\"id\": \""+user.getText().toString()+"\",\n" +
|
||||||
|
"\"firstname\": \""+fname.getText().toString()+"\",\n" +
|
||||||
|
"\"lastname\":\""+lname.getText().toString()+"\",\n" +
|
||||||
|
"\"email\":\""+mail.getText().toString()+"\",\n" +
|
||||||
|
"\"password\": \""+pwd.getText().toString()+"\",\n" +
|
||||||
|
"\"country\":\""+nacion.getText().toString()+"\",\n" +
|
||||||
|
"\"birthdate\": \""+datey.getText().toString()+"-"+datem.getText().toString()+"-"+dated.getText().toString()+"\"\n" +
|
||||||
|
"}";
|
||||||
|
// Enable writing
|
||||||
|
myConnection.setDoOutput(true);
|
||||||
|
// Write the data
|
||||||
|
myConnection.getOutputStream().write(myData.getBytes());
|
||||||
|
System.out.println(myConnection.getResponseCode());
|
||||||
|
if (myConnection.getResponseCode() == 201) {
|
||||||
|
success = true;
|
||||||
|
} else {
|
||||||
|
success = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
//Test
|
||||||
|
if (success) {
|
||||||
|
Toast.makeText(getApplicationContext(), "Registro exitoso", Toast.LENGTH_SHORT).show();
|
||||||
|
} else {
|
||||||
|
Toast.makeText(getApplicationContext(), "El usuario ya existe", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Toast.makeText(getApplicationContext(), "Conexión fallida", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
//Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show();
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
login.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue