-
-
Save geovaniggr/efdda37dc625b5f3ac2abaa4ecd135cc to your computer and use it in GitHub Desktop.
Conexão HTTP Simples e Serialização com Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gson.Gson; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.lang.reflect.Array; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
public class Main { | |
public static void main(String[] args) { | |
HttpURLConnection connection = null; | |
BufferedReader reader = null; | |
try { | |
URL url = new URL("https://api-pacientes.herokuapp.com/pacientes"); | |
connection = (HttpURLConnection) url.openConnection(); | |
connection.setRequestMethod("GET"); | |
reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); | |
StringBuilder response = new StringBuilder(); | |
String line; | |
while((line = reader.readLine()) != null){ | |
response.append(line); | |
} | |
Gson gson = new Gson(); | |
Pessoa[] pessoas = gson.fromJson(response.toString(), Pessoa[].class); | |
for( Pessoa pessoa : pessoas) { | |
System.out.println(pessoa); | |
} | |
} catch (MalformedURLException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
connection.disconnect(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Pessoa { | |
private String nome; | |
private int peso; | |
private double altura; | |
private int gordura; | |
private double imc; | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
public int getPeso() { | |
return peso; | |
} | |
public void setPeso(int peso) { | |
this.peso = peso; | |
} | |
public double getAltura() { | |
return altura; | |
} | |
public void setAltura(double altura) { | |
this.altura = altura; | |
} | |
public int getGordura() { | |
return gordura; | |
} | |
public void setGordura(int gordura) { | |
this.gordura = gordura; | |
} | |
public double getImc() { | |
return imc; | |
} | |
public void setImc(double imc) { | |
this.imc = imc; | |
} | |
@Override | |
public String toString() { | |
return "Pessoa{" + | |
"nome='" + nome + '\'' + | |
", peso=" + peso + | |
", altura=" + altura + | |
", gordura=" + gordura + | |
", imc=" + imc + | |
'}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.jsoup.Connection; | |
import org.jsoup.Jsoup; | |
import org.jsoup.nodes.Document; | |
import org.jsoup.select.Elements; | |
import java.io.IOException; | |
public class WebScrapping { | |
public static void main(String[] args) { | |
try { | |
Connection conn = Jsoup.connect("https://cursos.alura.com.br/forum/topico-buscando-dados-da-rede-129220"); | |
Document document = conn.get(); | |
Elements element = document.getElementsByClass("topic-post-content"); | |
System.out.println(element.select("p").first()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment