Advertisement
Guest User

Untitled

a guest
Apr 21st, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. CREATE TABLE tb_usuario(
  2. Id_usuario int auto_increment NOT NULL,
  3. Code Varchar(150) NOT NULL,
  4. User Varchar(90) NOT NULL,
  5. Pass Varchar(90) NOT NULL,
  6. TipoUsuario Varchar(90) NOT NULL,
  7. PRIMARY KEY(Id_usuario),
  8. UNIQUE KEY usuario_idx(Code)
  9. );
  10.  
  11. <?php
  12. require('conexion.php');
  13. $Code=$_GET['Code'];
  14. $User=$_GET['User'];
  15. $Pass=$_GET['Pass'];
  16. $TipoUsuario=$_GET['TipoUsuario'];
  17. // Sentencia INSERT
  18. $comando = "INSERT INTO tb_usuario ( " .
  19. "Code," .
  20. "User," .
  21. "Pass," .
  22. "TipoUsuario)" .
  23. " VALUES(?,?,?,?)";
  24.  
  25. // Preparar la sentencia
  26. $sentencia = $conn->prepare($comando);
  27. $sentencia->execute(array($Code,$User,$Pass,$TipoUsuario));
  28. if($sentencia)
  29. {
  30. // Código de éxito
  31. print json_encode(
  32. array(
  33. 'estado' => '1',
  34. 'mensaje' => 'Creación exitosa')
  35. );
  36.  
  37. }
  38. else
  39. {
  40. // Código de error
  41. print json_encode(
  42. array(
  43. 'estado' => '2',
  44. 'mensaje' => 'No se pudo realizar la inserción por que ya existe un registro con este código')
  45. );
  46. }
  47. ?>
  48.  
  49. final String Code = et1.getText().toString();
  50. final String User = et2.getText().toString();
  51. final String Pass = et3.getText().toString();
  52. final String TipoUsuario =muestraUser.getSelectedItem().toString();
  53.  
  54. HashMap<String, String> map = new HashMap<>();
  55.  
  56. map.put("Code",Code);
  57. map.put("User",User);
  58. map.put("Pass", Pass);
  59. map.put("TipoUsuario", TipoUsuario);
  60.  
  61. JSONObject jobject = new JSONObject(map);
  62.  
  63. Log.d(TAG, jobject.toString());
  64.  
  65.  
  66. VolleySingleton.getInstance(getApplication()).addToRequestQueue(
  67. new JsonObjectRequest(
  68. Request.Method.POST,
  69. Config.URL_ADD_USER,
  70. jobject,
  71. new Response.Listener<JSONObject>() {
  72. @Override
  73. public void onResponse(JSONObject response) {
  74.  
  75. procesarRespuesta(response);
  76. }
  77. },
  78. new Response.ErrorListener() {
  79. @Override
  80. public void onErrorResponse(VolleyError error) {
  81. Log.d(TAG, "Error Volley: " + error.getMessage());
  82. }
  83. }
  84.  
  85. ) {
  86. @Override
  87. public Map<String, String> getHeaders() {
  88. Map<String, String> headers = new HashMap<String, String>();
  89. headers.put("Content-Type", "application/json; charset=utf-8");
  90. headers.put("Accept", "application/json");
  91. return headers;
  92. }
  93.  
  94. @Override
  95. public String getBodyContentType() {
  96. return "application/json; charset=utf-8" + getParamsEncoding();
  97. }
  98. }
  99. );
  100.  
  101. }
  102.  
  103. private void procesarRespuesta(JSONObject response) {
  104.  
  105. try {
  106.  
  107. // Obtener estado
  108. String estado = response.getString("estado");
  109. // Obtener mensaje
  110. String mensaje = response.getString("mensaje");
  111.  
  112.  
  113. switch (estado) {
  114. case "success":
  115.  
  116. Toast.makeText(
  117. getApplication(),
  118. mensaje,
  119. Toast.LENGTH_LONG).show();
  120. break;
  121.  
  122. case "error":
  123.  
  124. Toast.makeText(
  125. getApplication(),
  126. mensaje,
  127. Toast.LENGTH_LONG).show();
  128. break;
  129. }
  130. } catch (JSONException e) {
  131. e.printStackTrace();
  132. }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement