0% found this document useful (0 votes)
15 views

MySQL Service Con Android

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

MySQL Service Con Android

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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:
conexion.php
<?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 ‘conexion.php’;

$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 AndroidManifest.xml 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="http://schemas.android.com/apk/res/android"
package="com.example.conexionmysql">

<uses-permission android:name="android.permission.INTERNET"></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/Theme.ConexionMySQL">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

6. Nos ubicamos en el archivo Gradle Scripts -> buil.gradle (Module: ConexionMySQL.app), y dentro de
Dependencias escribimos:

implementation 'com.android.volley:volley:1.1.1'
7. Ingresamos al archivo MainActivity.java para empezar a programar
package com.example.mysqlenandroid;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DownloadManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
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.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Map;

import static android.provider.ContactsContract.CommonDataKinds.Website.URL;

public class MainActivity extends AppCompatActivity {

EditText edtCodigo, edtProducto, edtPrecio, edtFabricante;


Button btnAgregar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

edtCodigo=(EditText)findViewById(R.id.edtCodigo);
edtProducto=(EditText)findViewById(R.id.edtProducto);
edtPrecio=(EditText)findViewById(R.id.edtPrecio);
edtFabricante=(EditText)findViewById(R.id.edtFabricante);
btnAgregar=(Button) findViewById(R.id.btnAgregar);

btnAgregar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

ejecutarServicio("http://192.168.1.46:8080/developeru/insertar_producto.php");
}
});
}
private void ejecutarServicio(String URL){
StringRequest stringRequest=new StringRequest(Request.Method.POST, URL,
new Response.Listener<String>(){
@Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(), "OPERACIÓN EXITOSA",
Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){

Toast.makeText(getApplicationContext(),error.toString(),Toast.LENGTH_SHORT).sho
w();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError{
Map<String, String> parametros=new HashMap<String, String>();
parametros.put("codigo",edtCodigo.getText().toString());
parametros.put("producto",edtProducto.getText().toString());
parametros.put("precio",edtPrecio.getText().toString());

parametros.put("fabricante",edtFabricante.getText().toString());
return super.getParams();
}
};
RequestQueue requestQueue= Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}

ANEXO DE INTERFAZ
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="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>

You might also like