Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.support.v4.app.FragmentManager;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- FragmentManager fm = getSupportFragmentManager();
- fm.beginTransaction().replace(R.id.escenario, new SesionFragment()).commit();
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/escenario"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.programming.narvaez.narvalogin.MainActivity">
- </android.support.constraint.ConstraintLayout>
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.TextView;
- public class Main2Activity extends AppCompatActivity {
- public static final String nombres="names";
- TextView txtBienvenido;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main2);
- txtBienvenido=(TextView)findViewById(R.id.txtbienvenido);
- String usuario=getIntent().getStringExtra("names");
- txtBienvenido.setText("¡Bienvenido "+ usuario + "!");
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.programming.narvaez.narvalogin.Main2Activity">
- <TextView
- android:id="@+id/txtbienvenido"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginTop="8dp"
- android:text="TextView"
- android:textColor="@android:color/background_dark"
- android:textSize="20sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.117" />
- </android.support.constraint.ConstraintLayout>
- import android.content.Context;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.android.volley.Request;
- import com.android.volley.RequestQueue;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.JsonObjectRequest;
- import com.android.volley.toolbox.JsonRequest;
- import com.android.volley.toolbox.Volley;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- public class SesionFragment extends Fragment implements Response.Listener<JSONObject>, Response.ErrorListener {
- RequestQueue rq;
- JsonRequest jrq;
- EditText txtUser, txtPwd;
- Button btnSesion, btnRegistrar;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View vista = inflater.inflate(R.layout.fragment_sesion, container, false);
- txtUser = (EditText) vista.findViewById(R.id.txtuser);
- txtPwd = (EditText) vista.findViewById(R.id.txtpwd);
- btnSesion = (Button) vista.findViewById(R.id.btnsesion);
- btnRegistrar = (Button) vista.findViewById(R.id.btnregistrar);
- rq = Volley.newRequestQueue(getContext());
- btnSesion.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- iniciar_sesion();
- }
- });
- btnRegistrar.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- registrar_usuario();
- }
- });
- // Inflate the layout for this fragment
- return vista;
- }
- @Override
- public void onErrorResponse(VolleyError error) {
- Toast.makeText(getContext(), "No Se encontró el usuario " +error.toString()+ txtUser.getText().toString(), Toast.LENGTH_LONG).show();
- }
- @Override
- public void onResponse(JSONObject response) {
- Toast.makeText(getContext(), "Se encontró el usuario " + txtUser.getText().toString(), Toast.LENGTH_SHORT).show();
- User usuario = new User();
- JSONArray jsonArray = response.optJSONArray("datos");
- JSONObject jsonObject = null;
- try {
- jsonObject = jsonArray.getJSONObject(0);
- usuario.setUser(jsonObject.optString("user"));
- usuario.setPwd(jsonObject.optString("pwd"));
- usuario.setNames(jsonObject.optString("names"));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- Intent intencion = new Intent(getContext(), Main2Activity.class);
- intencion.putExtra(Main2Activity.nombres, usuario.getNames());
- startActivity(intencion);
- }
- void iniciar_sesion() {
- //192.168.1.66(172.29.243.3
- String url = "http://192.168.1.66/login/sesion.php?user=" + txtUser.getText().toString() +
- "&pwd=" + txtPwd.getText().toString();
- jrq = new JsonObjectRequest(Request.Method.GET, url, null, this, this);
- rq.add(jrq);
- }
- void registrar_usuario(){
- RegistrarFragment fr=new RegistrarFragment();
- //fr.setArguments(fr);
- getActivity().getSupportFragmentManager().beginTransaction()
- .replace(R.id.escenario,fr)
- .addToBackStack(null)
- .commit();
- }
- }
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.programming.narvaez.narvalogin.SesionFragment">
- <!-- TODO: Update blank fragment layout -->
- <TextView
- android:id="@+id/textView"
- android:layout_width="0dp"
- android:layout_height="0dp"
- android:text="@string/hello_blank_fragment"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.0"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.0" />
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginLeft="16dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:text="@string/user_fragment"
- android:textColor="@android:color/background_dark"
- android:textSize="20sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.103"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="@+id/textView"
- app:layout_constraintVertical_bias="0.094" />
- <TextView
- android:id="@+id/textView3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginLeft="16dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:text="@string/pwd_fragment"
- android:textColor="@android:color/background_dark"
- android:textSize="20sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.03"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/textView2"
- app:layout_constraintVertical_bias="0.077" />
- <EditText
- android:id="@+id/txtuser"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:ems="10"
- android:inputType="textPersonName"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.275"
- app:layout_constraintStart_toEndOf="@+id/textView2"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.055" />
- <EditText
- android:id="@+id/txtpwd"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:ems="10"
- android:inputType="textPassword"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.81"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/txtuser"
- app:layout_constraintVertical_bias="0.021" />
- <Button
- android:id="@+id/btnsesion"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="232dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:text="@string/sesion_fragment"
- app:layout_constraintBottom_toBottomOf="@+id/textView"
- app:layout_constraintEnd_toEndOf="@+id/textView"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintStart_toStartOf="parent" />
- <Button
- android:id="@+id/btnregistrar"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginTop="8dp"
- android:text="@string/registrar_fragment"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/btnsesion"
- app:layout_constraintVertical_bias="0.315" />
- </android.support.constraint.ConstraintLayout>
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.android.volley.Request;
- import com.android.volley.RequestQueue;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.JsonObjectRequest;
- import com.android.volley.toolbox.JsonRequest;
- import com.android.volley.toolbox.Volley;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- public class RegistrarFragment extends Fragment implements Response.Listener<JSONObject>, Response.ErrorListener {
- RequestQueue rq;
- JsonRequest jrq;
- EditText txtUser, txtPwd, txtNames;
- Button btnSesion, btnRegistrar;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View vista = inflater.inflate(R.layout.fragment_registrar, container, false);
- txtUser = (EditText) vista.findViewById(R.id.txtuser);
- txtPwd = (EditText) vista.findViewById(R.id.txtpwd);
- txtNames= (EditText) vista.findViewById(R.id.txtnames);
- btnSesion = (Button) vista.findViewById(R.id.btnsesion);
- btnRegistrar = (Button) vista.findViewById(R.id.btnregistrar);
- rq = Volley.newRequestQueue(getContext());
- btnSesion.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- iniciar_sesion();
- }
- });
- btnRegistrar.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- registrar_usuario();
- }
- });
- // Inflate the layout for this fragment
- return vista;
- }
- @Override
- public void onErrorResponse(VolleyError error) {
- Toast.makeText(getContext(), "No Se pudó registrar el usuario " +error.toString()+ txtUser.getText().toString(), Toast.LENGTH_LONG).show();
- }
- @Override
- public void onResponse(JSONObject response) {
- Toast.makeText(getContext(), "Se ha registrado el usuario " + txtUser.getText().toString(), Toast.LENGTH_SHORT).show();
- limpiarCajas();
- }
- void limpiarCajas() {
- txtNames.setText("");
- txtUser.setText("");
- txtPwd.setText("");
- }
- void iniciar_sesion() {
- SesionFragment fr=new SesionFragment();
- //fr.setArguments(bn);
- getActivity().getSupportFragmentManager().beginTransaction()
- .replace(R.id.escenario,fr)
- .addToBackStack(null)
- .commit();
- }
- void registrar_usuario(){
- //192.168.1.66(172.29.243.3
- String url = "http://192.168.1.66/login/registrar.php?names=" +txtNames.getText().toString()+"&user="+ txtUser.getText().toString() +
- "&pwd=" + txtPwd.getText().toString();
- jrq = new JsonObjectRequest(Request.Method.GET, url, null, this, this);
- rq.add(jrq);
- }
- }
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.programming.narvaez.narvalogin.SesionFragment">
- <!-- TODO: Update blank fragment layout -->
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginLeft="16dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:text="@string/user_fragment"
- android:textColor="@android:color/background_dark"
- android:textSize="20sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.153"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.267" />
- <EditText
- android:id="@+id/txtuser"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginRight="16dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:ems="10"
- android:inputType="textPersonName"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.947"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.236" />
- <TextView
- android:id="@+id/textView3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginLeft="16dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:text="@string/pwd_fragment"
- android:textColor="@android:color/background_dark"
- android:textSize="20sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.123"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.416" />
- <EditText
- android:id="@+id/txtpwd"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginRight="16dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:ems="10"
- android:inputType="textPassword"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="1.0"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.407" />
- <Button
- android:id="@+id/btnsesion"
- android:layout_width="123dp"
- android:layout_height="50dp"
- android:layout_marginBottom="8dp"
- android:layout_marginStart="128dp"
- android:layout_marginTop="8dp"
- android:text="@string/sesion_fragment"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.721" />
- <Button
- android:id="@+id/btnregistrar"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginTop="8dp"
- android:text="@string/registrar_fragment"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.502"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.577" />
- <TextView
- android:id="@+id/textView4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="24dp"
- android:layout_marginLeft="16dp"
- android:layout_marginTop="8dp"
- android:text="@string/names_fragment"
- android:textColor="@android:color/background_dark"
- android:textSize="22sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toStartOf="@+id/txtnames"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.131"
- tools:text="@string/names_fragment" />
- <EditText
- android:id="@+id/txtnames"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginRight="16dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:ems="10"
- android:inputType="textPersonName"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.947"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.1" />
- <TextView
- android:id="@+id/textView5"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginTop="8dp"
- android:text="@string/registrar_fragment"
- android:textColor="@color/colorPrimary"
- android:textSize="24sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.016" />
- </android.support.constraint.ConstraintLayout>
- /**
- * Created by NARVAEZ on 17/05/2018.
- */
- public class User {
- private String names, user, pwd;
- public String getNames() {
- return names;
- }
- public void setNames(String names) {
- this.names = names;
- }
- public String getUser() {
- return user;
- }
- public void setUser(String user) {
- this.user = user;
- }
- public String getPwd() {
- return pwd;
- }
- public void setPwd(String pwd) {
- this.pwd = pwd;
- }
- }
- <resources>
- <string name="app_name">Narváez Sesión</string>
- <!-- TODO: Remove or change this placeholder text -->
- <string name="hello_blank_fragment">Inicio de Sesión</string>
- <string name="user_fragment">Usuario:</string>
- <string name="pwd_fragment">Contraseña:</string>
- <string name="sesion_fragment">Inciar Sesión</string>
- <string name="registrar_fragment">Registrar usuario</string>
- <string name="names_fragment">Nombres: </string>
- </resources>
- compile 'com.android.volley:volley:1.1.0'
- configurations.all {
- resolutionStrategy.force 'com.android.support:support-annotations:23.1.0'
- }
- <uses-permission android:name="android.permission.INTERNET"/>
- <?PHP
- $hostname="localhost";
- $database="tu_bd";
- $username="root";
- $password="";
- $json=array();
- if(isset($_GET["names"])&&($_GET["user"]) && isset($_GET["pwd"])){
- $names=$_GET['names']
- $user=$_GET['user'];
- $pwd=$_GET['pwd'];
- $conexion=mysqli_connect($hostname,$username,$password,$database);
- $consulta="INSERT INTO usuarios(names, user, pwd) VALUES ('{$names}','{$user}' , '{$pwd}')";
- $resultado=mysqli_query($conexion,$consulta);
- if($consulta){
- $consulta="SELECT * FROM usuarios WHERE names='{$names}'";
- $resultado=mysqli_query($conexion,$consulta);
- if($reg=mysqli_fetch_array($resultado)){
- $json['datos'][]=$reg;
- }
- mysqli_close($conexion);
- echo json_encode($json);
- }
- else{
- $results["names"]='';
- $results["user"]='';
- $results["pwd"]='';
- $json['datos'][]=$results;
- echo json_encode($json);
- }
- }
- else{
- $results["names"]='';
- $results["user"]='';
- $results["pwd"]='';
- $json['datos'][]=$results;
- echo json_encode($json);
- }
- ?>
Add Comment
Please, Sign In to add comment