Created
March 24, 2017 01:19
-
-
Save jacksonfdam/bff6cd7b70a78e8309e9e222189fddc3 to your computer and use it in GitHub Desktop.
Atualizador
This file contains hidden or 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
package br.com.targettrust.aula0801; | |
import android.util.Log; | |
import org.json.JSONArray; | |
import org.json.JSONObject; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by instrutor on 23/03/2017. | |
*/ | |
public class Atualizador extends Thread{ | |
List<Pessoa> lista = new ArrayList<Pessoa>(); | |
public Atualizador(){ | |
} | |
public List<Pessoa> getListaPessoas(){ | |
return lista; | |
} | |
public void run(){ | |
String linha; | |
String resultado = ""; | |
try { | |
URL url = new URL("https://randomuser.me/api/?results=5"); | |
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); | |
while ((linha = in.readLine()) != null) { | |
resultado += linha; | |
} | |
in.close(); | |
Log.v("Thread", resultado); | |
JSONObject ResultadosAPI = new JSONObject(resultado); | |
JSONArray chaveResultados = ResultadosAPI.getJSONArray("results"); | |
Log.v("JSON", "Possui " + chaveResultados.length() + " resultados."); | |
for (int pos = 0; pos < chaveResultados.length(); pos++ ) { | |
JSONObject usuario = chaveResultados.getJSONObject(pos); | |
Pessoa pessoa = new Pessoa(); | |
pessoa.nome = usuario.getJSONObject("name").get("first").toString() + " " + usuario.getJSONObject("name").get("last").toString(); | |
pessoa.foto = usuario.getJSONObject("picture").get("large").toString(); | |
pessoa.email = usuario.getString("email"); | |
pessoa.genero = usuario.getString("gender"); | |
lista.add(pessoa); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment