Advertisement
toujoursseule

Untitled

Dec 1st, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. package pe.edu.idat.semana12httpurlconnection.BL;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7.  
  8. /**
  9. * Created by Administrador on 01/12/2017.
  10. */
  11.  
  12. public class UsuarioBL {
  13.  
  14. public String listarUsuarios(String newUrl){
  15. StringBuilder totallinea = new StringBuilder();
  16. try {
  17. URL obj = new URL(newUrl);
  18. HttpURLConnection conn =(HttpURLConnection) obj.openConnection();
  19. //conn.setRequestMethod("GET"); //POST PUT DELETE
  20. conn.setRequestProperty("Accept", "application/json");
  21. conn.setUseCaches(false);
  22. conn.setRequestProperty("Accept-Enconding","grip"); //ACEPTE INFORMACION COMPRIMIDA
  23. conn.setReadTimeout(15 * 1000); //PARA QUE ESPERE HASTA 15 SEGUNDOS Y CIERRA
  24. conn.connect();
  25. //conn.setRequestProperty("Content-type","application/json"); //ES PARA ENVIAR INFORMACION
  26.  
  27. BufferedReader in = new BufferedReader(
  28. new InputStreamReader(conn.getInputStream(), "UTF-8")); //DEVUELVE COMO UN STRING
  29. // Y HACE LA LECTURA DE LINEA POR LINEA
  30. String linea;
  31.  
  32. while((linea = in.readLine())!=null){
  33. totallinea .append(linea);
  34.  
  35. }
  36. in.close();
  37.  
  38.  
  39. } catch (Exception e){
  40. e.printStackTrace();
  41. }
  42.  
  43.  
  44. return totallinea.toString();
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement