Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $consulta=$_GET["Code"]
- try{
- $usuario = "xxxxxxx";
- $password = "xxxxx";
- $conn = new PDO('mysql:host=xxxxxx;dbname=xxxxx', $usuario, $password);
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $conn->exec("SET CHARACTER SET utf8");
- $sql="SELECT Code,user,Pass,TipoUsuario FROM Usuarios WHERE Code= :Code";
- $resultado=$conn->prepare($sql);
- $resultado ->execute(array($consulta));
- $registro=$resultado->fetchAll(PDO::FETCH_ASSOC);
- $json=json_encode($registro);
- return $json;
- }catch(PDOException $e){
- echo 'No existe un usuario con este código';
- }
- ?>
- //URL DE MI CÓDIGO PHP
- public static final String URL_ADD_USER="http://ejemplo.atwebpages.com/conect/addUser.php";
- public static final String URL_GET_USER = "http://ejemplo.atwebpages.com/conect/busquedaUser.php?Code=";
- // obtengo valores
- public static final String KEY_USER_CODE = "Code";
- public static final String KEY_USER_USUARIO = "user";
- public static final String KEY_USER_PASS = "Pass";
- public static final String KEY_USER_TIPOUSER = "TipoUsuario";
- // El result
- public static final String TAG_JSON_ARRAY="result";
- private void getData() {
- String Code= et1.getText().toString().trim();
- if (Code.equals("")) {
- Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
- return;
- }
- loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
- String url = Config.URL_GET_USER+et1.getText().toString().trim();
- StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- loading.dismiss();
- showJSON(response);
- }
- },
- new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Toast.makeText(buscarUpdateUser.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
- }
- });
- RequestQueue requestQueue = Volley.newRequestQueue(this);
- requestQueue.add(stringRequest);
- }
- private void showJSON(String response){
- try {
- JSONObject jsonObject = new JSONObject(response);
- JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
- JSONObject collegeData = result.getJSONObject(0);
- String User = collegeData.getString(Config.KEY_USER_USUARIO);
- String Pass = collegeData.getString(Config.KEY_USER_PASS);
- String TipoUsuario = collegeData.getString(Config.KEY_USER_TIPOUSER);
- et2.setText(User);
- et3.setText(Pass);
- SeleccionaItemTipoUsuario(tipUser,TipoUsuario);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- public void onResponse(String response) {
- showJSON(response);
- JSONObject jsonObject = new JSONObject(response);
- {"menu": {
- "id": "file",
- "value": "File",
- "popup": {
- "menuitem": [
- {"value": "New", "onclick": "CreateNewDoc()"},
- {"value": "Open", "onclick": "OpenDoc()"},
- {"value": "Close", "onclick": "CloseDoc()"}
- ]
- }
- }}
- $sql="SELECT Code,user,Pass,TipoUsuario FROM Usuarios";
- $resultado=$conn->prepare($sql);
- $resultado ->execute();
- while($registro=$resultado->fetch(PDO::FETCH_ASSOC))
- {
- echo Code.$registro[0];
- echo user. $registro[1];
- echo Pass.$registro[2];
- echo TipoUsuario.$registro[3];
- $sql="SELECT Code,user,Pass,TipoUsuario FROM Usuarios";
- $resultado=$conn->prepare($sql);
- $resultado ->execute();
- /*
- * Esto sobra
- * while($registro=$resultado->fetch(PDO::FETCH_ASSOC))
- * {
- * echo Code.$registro[0];
- * echo user. $registro[1];
- * echo Pass.$registro[2];
- * echo TipoUsuario.$registro[3];
- /*
- * Si Android espera un JSON, lo construyes aquí
- * Simplemente convirtiendo los datos recibidos
- * a JSON, usando json_encode
- *
- */
- $registro=$resultado->fetchAll(PDO::FETCH_ASSOC);
- $json=json_encode($registro);
- /* Esta línea es muyyyy importante. Tu código devuelve el JSON a Android */
- return $json;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement