MySQL Service con Android – Web Service
1. Creamos una base de datos: deloperu
2. Creamos la tabla: producto
codigo (var 4)
producto – (var 50)
precio (dec 6,2)
fabricante (var 25)
3. Creamos las web service:
[Link]
<?php
$hostname = ‘localhost’;
$database = ‘developeru_db’;
$username = ‘root’;
$password = ‘ ‘;
$conexion = new mysqli($hostname, $username, $password, $database);
if ($conexion -> connect_errno){
echo “Lo sentimos, el sitio web está experimentando problemas”;
}
?>
insertar_producto.php
<?php
Include ‘[Link]’;
$codigo = $_POST[‘codigo’];
$producto = $_POST[‘producto’];
$precio = $_POST[‘precio’];
$fabricante = $_POST[‘fabricante’];
$consulta = “insert into producto values (‘”.$codigo.”’, ‘”.$producto.”’, ‘”.precio.”’, ‘”.fabricante.”’)”;
mysqli_query($conexion, $consulta) or die (mysqli_error( ));
mysqli_close($conexion);
?>
4. Realizamos la siguiente interfaz
5. En el Android Studio ingresamos en el archivo [Link] para solicitar los permisos de
conexión a Internet para conectarnos con los web service.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
package="[Link]">
<uses-permission android:name="[Link]"></uses-
permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/[Link]">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
6. Nos ubicamos en el archivo Gradle Scripts -> [Link] (Module: [Link]), y dentro de
Dependencias escribimos:
implementation '[Link]:volley:1.1.1'
7. Ingresamos al archivo [Link] para empezar a programar
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import static [Link];
public class MainActivity extends AppCompatActivity {
EditText edtCodigo, edtProducto, edtPrecio, edtFabricante;
Button btnAgregar;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
edtCodigo=(EditText)findViewById([Link]);
edtProducto=(EditText)findViewById([Link]);
edtPrecio=(EditText)findViewById([Link]);
edtFabricante=(EditText)findViewById([Link]);
btnAgregar=(Button) findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
ejecutarServicio("[Link]
}
});
}
private void ejecutarServicio(String URL){
StringRequest stringRequest=new StringRequest([Link], URL,
new [Link]<String>(){
@Override
public void onResponse(String response) {
[Link](getApplicationContext(), "OPERACIÓN EXITOSA",
Toast.LENGTH_SHORT).show();
}
}, new [Link](){
@Override
public void onErrorResponse(VolleyError error){
[Link](getApplicationContext(),[Link](),Toast.LENGTH_SHORT).sho
w();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError{
Map<String, String> parametros=new HashMap<String, String>();
[Link]("codigo",[Link]().toString());
[Link]("producto",[Link]().toString());
[Link]("precio",[Link]().toString());
[Link]("fabricante",[Link]().toString());
return [Link]();
}
};
RequestQueue requestQueue= [Link](this);
[Link](stringRequest);
}
}
ANEXO DE INTERFAZ
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="20dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="REGISTRO PRODUCTO"
android:textColor="#0A1E88"
android:textSize="26sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingTop="30dp"
android:paddingRight="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<EditText
android:id="@+id/edtCodigo"
android:layout_width="223dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:gravity="center"
android:hint="Ingrese el código"
android:inputType="textPersonName"
android:textSize="22sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/btnBuscar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="Buscar"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
<EditText
android:id="@+id/edtProducto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:ems="10"
android:gravity="center"
android:hint="Ingrese el Producto"
android:inputType="textPersonName"
android:textSize="22sp" />
<EditText
android:id="@+id/edtPrecio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:ems="10"
android:gravity="center"
android:hint="Ingrese precio"
android:inputType="textPersonName"
android:textSize="22sp" />
<EditText
android:id="@+id/edtFabricante"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:ems="10"
android:gravity="center"
android:hint="Ingrese fabricante"
android:inputType="textPersonName"
android:textSize="22sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="vertical">
<Button
android:id="@+id/btnAgregar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="Agregar"
android:textSize="20sp" />
<Button
android:id="@+id/btnEditar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="Editar"
android:textSize="20sp" />
<Button
android:id="@+id/btnEliminar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="Eliminar"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>