Guest User

Untitled

a guest
Oct 19th, 2018
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. <?php
  2. $servidor="localhost";
  3. $nombre_bd="test2";
  4. $username="root";
  5. $password="";
  6. $conexion = mysql_connect($servidor, $username, $password) or die ("No se ha podido conectar al servidor de Base de datos");
  7.  
  8. mysql_select_db($nombre_bd, $conexion) or die ("xD");
  9. $sql = "SELECT * FROM usuario";
  10. $result = mysql_query($sql, $conexion);
  11. $array = array();
  12. $i = 0;
  13. while($row = mysql_fetch_array($result)) {
  14.  $array[$i] = $row;
  15.  $i ++;
  16. }
  17. $json = json_encode($array, JSON_FORCE_OBJECT);
  18. echo $json;
  19. ?>
  20.  
  21. /*
  22. * To change this license header, choose License Headers in Project Properties.
  23. * To change this template file, choose Tools | Templates
  24. * and open the template in the editor.
  25. */
  26. package prueba.comsumir.webservice;
  27.  
  28. import com.google.gson.Gson;
  29. import java.io.BufferedReader;
  30. import java.io.IOException;
  31. import java.io.InputStreamReader;
  32. import java.net.URL;
  33. import java.net.URLConnection;
  34.  
  35. /**
  36. *
  37. * @author Pablo
  38. */
  39. public class PruebaComsumirWebservice {
  40.  
  41.    /**
  42.     * @param args the command line arguments
  43.     */
  44.    public static void main(String[] args) {
  45.        Usuario usuario = new Usuario(1, "User1", "123");
  46.        System.out.println(usuario.toString());
  47.        URL url;
  48.        try {
  49.            // Creando un objeto URL
  50.            url = new URL("http://localhost:8080/Sitio%201/code.php");
  51.  
  52.            // Realizando la petición GET
  53.            URLConnection con = url.openConnection();
  54.  
  55.            // Leyendo el resultado
  56.            BufferedReader in = new BufferedReader(new InputStreamReader(
  57.                    con.getInputStream()));
  58.  
  59.            String linea;
  60.            String json = "";
  61.            while ((linea = in.readLine()) != null) {
  62.                System.out.println(linea);
  63.                json += linea;
  64.            }
  65.            //{"id":1,"usuario":"usuario1","contrasenia":"asd"}
  66.            Usuario usuario1 = new Gson().fromJson(json, Usuario.class);
  67.            System.out.println(usuario.getUsuario());
  68.            
  69.        } catch (IOException e) {
  70.            System.out.println(e.getMessage());
  71.        }
  72.    }
  73.    
  74.  
  75. /*
  76. * To change this license header, choose License Headers in Project Properties.
  77. * To change this template file, choose Tools | Templates
  78. * and open the template in the editor.
  79. */
  80. package prueba.comsumir.webservice;
  81.  
  82. /**
  83. *
  84. * @author Pablo
  85. */
  86. public class Usuario {
  87.    private int id;
  88.    private String usuario;
  89.    private String contrasenia;
  90.  
  91.    public Usuario(int id, String usuario, String contrasenia) {
  92.        this.id = id;
  93.        this.usuario = usuario;
  94.        this.contrasenia = contrasenia;
  95.    }
  96.  
  97.    public int getId() {
  98.        return id;
  99.    }
  100.  
  101.    public void setId(int id) {
  102.        this.id = id;
  103.    }
  104.  
  105.    public String getUsuario() {
  106.        return usuario;
  107.    }
  108.  
  109.    public void setUsuario(String usuario) {
  110.        this.usuario = usuario;
  111.    }
  112.  
  113.    public String getContrasenia() {
  114.        return contrasenia;
  115.    }
  116.  
  117.    public void setContrasenia(String contrasenia) {
  118.        this.contrasenia = contrasenia;
  119.    }
  120.  
  121.    @Override
  122.    public String toString() {
  123.        return String.format("id:%s, usuario:%s, contrasenia:%s", id, usuario, contrasenia);
  124.    }
  125. }
  126.  
  127. {"0":{"0":"1","id":"1","1":"user1","usuario":"user1","2":"asd","contrasenia":"asd"},"1":{"0":"2","id":"2","1":"user2","usuario":"user2","2":"dsa","contrasenia":"dsa"}}
  128. Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 169 path $
  129. at com.google.gson.Gson.assertFullConsumption(Gson.java:855)
  130. at com.google.gson.Gson.fromJson(Gson.java:845)
  131. at com.google.gson.Gson.fromJson(Gson.java:793)
  132. at com.google.gson.Gson.fromJson(Gson.java:765)
  133. at prueba.comsumir.webservice.PruebaComsumirWebservice.main(PruebaComsumirWebservice.java:48)
  134. Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 169 path $
  135. at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1567)
  136. at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1416)
  137. at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:546)
  138. at com.google.gson.stream.JsonReader.peek(JsonReader.java:429)
  139. at com.google.gson.Gson.assertFullConsumption(Gson.java:851)
  140. ... 4 more
  141. C:UsersPabloAppDataLocalNetBeansCache8.2executor-snippetsrun.xml:53: Java returned: 1
  142. BUILD FAILED (total time: 1 second)
  143.  
  144. $con=mysqli_connect($servidor,$username,$password,$nombre_bd);
  145.  
  146. if(!$con) die('error conectando');
  147.  
  148. $sql = "SELECT * FROM usuario";
  149. $result = mysqli_query($con, $sql);
  150.  
  151. $rtn = array();
  152.  
  153. if($result){
  154. $row=mysqli_fetch_assoc($result);
  155. while($row !== null){
  156. $rtn[] = $row;
  157. $row=mysqli_fetch_assoc($result);
  158. }
  159. mysqli_close($con);
  160. }
  161.  
  162. header('Content-Type: application/json');
  163. return json_encode($rtn);
  164.  
  165. public class Usuarios{
  166. private List<Usuario> usuarios;
  167. // getter & setter
  168. }
  169.  
  170. //{"id":1, "usuario":"usuario1", "contrasenia":"asd"}
  171. Usuarios usuarios = new Gson().fromJson(json, Usuarios.class);
Add Comment
Please, Sign In to add comment