Advertisement
Guest User

Untitled

a guest
Jul 16th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. <?php
  2. $consulta=$_GET["Code"]
  3. try{
  4. $usuario = "xxxxxxx";
  5. $password = "xxxxx";
  6. $conn = new PDO('mysql:host=xxxxxx;dbname=xxxxx', $usuario, $password);
  7. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  8. $conn->exec("SET CHARACTER SET utf8");
  9.  
  10. $sql="SELECT Code,user,Pass,TipoUsuario FROM Usuarios WHERE Code= :Code";
  11. $resultado=$conn->prepare($sql);
  12. $resultado ->execute(array($consulta));
  13. $registro=$resultado->fetchAll(PDO::FETCH_ASSOC);
  14. $json=json_encode($registro);
  15.  
  16. return $json;
  17.  
  18. }catch(PDOException $e){
  19. echo 'No existe un usuario con este código';
  20. }
  21. ?>
  22.  
  23. //URL DE MI CÓDIGO PHP
  24. public static final String URL_ADD_USER="http://ejemplo.atwebpages.com/conect/addUser.php";
  25. public static final String URL_GET_USER = "http://ejemplo.atwebpages.com/conect/busquedaUser.php?Code=";
  26.  
  27.  
  28. // obtengo valores
  29. public static final String KEY_USER_CODE = "Code";
  30. public static final String KEY_USER_USUARIO = "user";
  31. public static final String KEY_USER_PASS = "Pass";
  32. public static final String KEY_USER_TIPOUSER = "TipoUsuario";
  33.  
  34. // El result
  35. public static final String TAG_JSON_ARRAY="result";
  36.  
  37. private void getData() {
  38. String Code= et1.getText().toString().trim();
  39. if (Code.equals("")) {
  40. Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
  41. return;
  42. }
  43. loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
  44.  
  45. String url = Config.URL_GET_USER+et1.getText().toString().trim();
  46.  
  47. StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
  48. @Override
  49. public void onResponse(String response) {
  50. loading.dismiss();
  51. showJSON(response);
  52. }
  53. },
  54. new Response.ErrorListener() {
  55. @Override
  56. public void onErrorResponse(VolleyError error) {
  57. Toast.makeText(buscarUpdateUser.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
  58. }
  59. });
  60.  
  61. RequestQueue requestQueue = Volley.newRequestQueue(this);
  62. requestQueue.add(stringRequest);
  63. }
  64. private void showJSON(String response){
  65. try {
  66. JSONObject jsonObject = new JSONObject(response);
  67. JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
  68. JSONObject collegeData = result.getJSONObject(0);
  69. String User = collegeData.getString(Config.KEY_USER_USUARIO);
  70. String Pass = collegeData.getString(Config.KEY_USER_PASS);
  71. String TipoUsuario = collegeData.getString(Config.KEY_USER_TIPOUSER);
  72.  
  73. et2.setText(User);
  74. et3.setText(Pass);
  75. SeleccionaItemTipoUsuario(tipUser,TipoUsuario);
  76.  
  77. } catch (JSONException e) {
  78. e.printStackTrace();
  79. }
  80. }
  81.  
  82. public void onResponse(String response) {
  83.  
  84. showJSON(response);
  85.  
  86. JSONObject jsonObject = new JSONObject(response);
  87.  
  88. {"menu": {
  89. "id": "file",
  90. "value": "File",
  91. "popup": {
  92. "menuitem": [
  93. {"value": "New", "onclick": "CreateNewDoc()"},
  94. {"value": "Open", "onclick": "OpenDoc()"},
  95. {"value": "Close", "onclick": "CloseDoc()"}
  96. ]
  97. }
  98. }}
  99.  
  100. $sql="SELECT Code,user,Pass,TipoUsuario FROM Usuarios";
  101. $resultado=$conn->prepare($sql);
  102. $resultado ->execute();
  103. while($registro=$resultado->fetch(PDO::FETCH_ASSOC))
  104. {
  105. echo Code.$registro[0];
  106. echo user. $registro[1];
  107. echo Pass.$registro[2];
  108. echo TipoUsuario.$registro[3];
  109.  
  110. $sql="SELECT Code,user,Pass,TipoUsuario FROM Usuarios";
  111. $resultado=$conn->prepare($sql);
  112. $resultado ->execute();
  113. /*
  114. * Esto sobra
  115. * while($registro=$resultado->fetch(PDO::FETCH_ASSOC))
  116. * {
  117. * echo Code.$registro[0];
  118. * echo user. $registro[1];
  119. * echo Pass.$registro[2];
  120. * echo TipoUsuario.$registro[3];
  121.  
  122.  
  123. /*
  124. * Si Android espera un JSON, lo construyes aquí
  125. * Simplemente convirtiendo los datos recibidos
  126. * a JSON, usando json_encode
  127. *
  128. */
  129. $registro=$resultado->fetchAll(PDO::FETCH_ASSOC);
  130. $json=json_encode($registro);
  131.  
  132. /* Esta línea es muyyyy importante. Tu código devuelve el JSON a Android */
  133. return $json;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement