Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE TABLE tb_usuario(
- Id_usuario int auto_increment NOT NULL,
- Code Varchar(150) NOT NULL,
- User Varchar(90) NOT NULL,
- Pass Varchar(90) NOT NULL,
- TipoUsuario Varchar(90) NOT NULL,
- PRIMARY KEY(Id_usuario),
- UNIQUE KEY usuario_idx(Code)
- );
- <?php
- require('conexion.php');
- $Code=$_GET['Code'];
- $User=$_GET['User'];
- $Pass=$_GET['Pass'];
- $TipoUsuario=$_GET['TipoUsuario'];
- // Sentencia INSERT
- $comando = "INSERT INTO tb_usuario ( " .
- "Code," .
- "User," .
- "Pass," .
- "TipoUsuario)" .
- " VALUES(?,?,?,?)";
- // Preparar la sentencia
- $sentencia = $conn->prepare($comando);
- $sentencia->execute(array($Code,$User,$Pass,$TipoUsuario));
- if($sentencia)
- {
- // Código de éxito
- print json_encode(
- array(
- 'estado' => '1',
- 'mensaje' => 'Creación exitosa')
- );
- }
- else
- {
- // Código de error
- print json_encode(
- array(
- 'estado' => '2',
- 'mensaje' => 'No se pudo realizar la inserción por que ya existe un registro con este código')
- );
- }
- ?>
- final String Code = et1.getText().toString();
- final String User = et2.getText().toString();
- final String Pass = et3.getText().toString();
- final String TipoUsuario =muestraUser.getSelectedItem().toString();
- HashMap<String, String> map = new HashMap<>();
- map.put("Code",Code);
- map.put("User",User);
- map.put("Pass", Pass);
- map.put("TipoUsuario", TipoUsuario);
- JSONObject jobject = new JSONObject(map);
- Log.d(TAG, jobject.toString());
- VolleySingleton.getInstance(getApplication()).addToRequestQueue(
- new JsonObjectRequest(
- Request.Method.POST,
- Config.URL_ADD_USER,
- jobject,
- new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- procesarRespuesta(response);
- }
- },
- new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Log.d(TAG, "Error Volley: " + error.getMessage());
- }
- }
- ) {
- @Override
- public Map<String, String> getHeaders() {
- Map<String, String> headers = new HashMap<String, String>();
- headers.put("Content-Type", "application/json; charset=utf-8");
- headers.put("Accept", "application/json");
- return headers;
- }
- @Override
- public String getBodyContentType() {
- return "application/json; charset=utf-8" + getParamsEncoding();
- }
- }
- );
- }
- private void procesarRespuesta(JSONObject response) {
- try {
- // Obtener estado
- String estado = response.getString("estado");
- // Obtener mensaje
- String mensaje = response.getString("mensaje");
- switch (estado) {
- case "success":
- Toast.makeText(
- getApplication(),
- mensaje,
- Toast.LENGTH_LONG).show();
- break;
- case "error":
- Toast.makeText(
- getApplication(),
- mensaje,
- Toast.LENGTH_LONG).show();
- break;
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement