Created
February 23, 2012 18:43
-
-
Save alexandregama/1894280 to your computer and use it in GitHub Desktop.
JSON Simples e Prático, Parte II
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
/* Código que faz parte do artigo http://alexandregama.wordpress.com/2012/02/23/json-simples-e-pratico-parte-ii/ */ | |
/* Classe Http */ | |
package br.com.artigo.json; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.URL; | |
public class Http { | |
public String chamaUrl(String url) throws IOException { | |
URL facebook = new URL(url); | |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(facebook.openStream())); | |
String retornoJson; | |
StringBuilder builder = new StringBuilder(); | |
while ((retornoJson = bufferedReader.readLine()) != null) | |
builder.append(retornoJson); | |
bufferedReader.close(); | |
return builder.toString(); | |
} | |
} | |
/* Classe Facebook */ | |
package br.com.artigo.json; | |
public class Facebook { | |
private String id; | |
private String name; | |
private String category; | |
public String getId() { | |
return id; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getCategory() { | |
return category; | |
} | |
public void setCategory(String category) { | |
this.category = category; | |
} | |
@Override | |
public String toString() { | |
return "Facebook [id=" + id + ", name=" + name + ", category=" | |
+ category + "]"; | |
} | |
} | |
/* Classe ArtigoJson */ | |
package br.com.artigo.json; | |
import java.io.IOException; | |
import java.util.Map; | |
import net.sf.json.JSONObject; | |
public class ArtigoJson { | |
private static final String HTTPS_GRAPH_FACEBOOK = "https://graph.facebook.com/98423808305"; | |
@SuppressWarnings("unchecked") | |
public static void main(String[] args) throws IOException { | |
//Retorno JSON | |
JSONObject object = chamadaHttp(); | |
System.out.println(object); | |
//Retorno dos dados que precisamos | |
String id = (String) object.get("id"); | |
Map map = (Map) object.get("from"); //Que interessante! Um cast para um Map! | |
String name = map.get("name"); | |
String category = map.get("category"); | |
//Populando o objeto facebook para persistencia | |
Facebook facebook = new Facebook(); | |
facebook.setId(id); | |
facebook.setName(name); | |
facebook.setCategory(category); | |
//Impressão do objeto | |
System.out.println(facebook); | |
} | |
private static JSONObject chamadaHttp() throws IOException { | |
Http http = new Http(); | |
String retornoJson = http.chamaUrl(HTTPS_GRAPH_FACEBOOK); | |
JSONObject objetoJson = JSONObject.fromObject(retornoJson); | |
return objetoJson; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment