MySQL Service Con Android
MySQL Service Con Android
insertar_producto.php
<?php
Include ‘conexion.php’;
$codigo = $_POST[‘codigo’];
$producto = $_POST[‘producto’];
$precio = $_POST[‘precio’];
$fabricante = $_POST[‘fabricante’];
<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" />
</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;
@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>