Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. public String acessar(String endereco) throws IOException {
  2. URL url = new URL("http://pokeapi.co/api/v2/pokemon/1/");
  3. HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  4. urlConnection.setRequestMethod("GET");
  5. urlConnection.connect();
  6. InputStream inputStream = urlConnection.getInputStream();
  7. if (inputStream == null) {
  8. return null;
  9. }
  10. BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
  11. String linha;
  12. StringBuffer buffer = new StringBuffer();
  13. while ((linha = reader.readLine()) != null) {
  14. buffer.append(linha + "n");
  15. }
  16. if (buffer.length() == 0) {
  17. return null;
  18. }
  19. if (urlConnection != null) {
  20. urlConnection.disconnect();
  21. }
  22. if (reader != null) {
  23. try {
  24. reader.close();
  25. } catch (final IOException e) {
  26. Log.e("Erro", "Erro fechando o stream", e);
  27. }
  28. }
  29. return "";
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement