PageRenderTime 69ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/br/com/constantini/test/Test.java

https://github.com/ulissesc/yql-for-java
Java | 100 lines | 68 code | 30 blank | 2 comment | 4 complexity | b61ceb5d1b0326f658b2312c802983fb MD5 | raw file
  1. package br.com.constantini.test;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.net.URL;
  6. import java.net.URLConnection;
  7. import java.util.List;
  8. import br.com.constantini.yql.Item;
  9. import br.com.constantini.yql.Results;
  10. import br.com.constantini.yql.YQLQuery;
  11. import com.thoughtworks.xstream.XStream;
  12. public class Test {
  13. public static void main(String[] args) throws IOException {
  14. /* TESTE DE CRIACAO DO OBJETO */
  15. createQuery();
  16. /* TESTE DE RECUPERACAO DO OBJETO VIA HTTP */
  17. getHttpObject();
  18. }
  19. private static void getHttpObject() throws IOException {
  20. XStream stream = new XStream();
  21. stream.autodetectAnnotations(true);
  22. stream.omitField(Item.class, "pubDate");
  23. stream.omitField(Item.class, "guid");
  24. stream.omitField(Item.class, "dc:creator");
  25. stream.omitField(Item.class, "wfw:commentRss");
  26. stream.omitField(Item.class, "source");
  27. stream.omitField(Item.class, "comments");
  28. String xmlRequest = httpRequest("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20delicious.feeds%20where%20username%20%3D%20%20%22torquatro%22&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys" );
  29. stream.alias("query", YQLQuery.class);
  30. YQLQuery retorno = (YQLQuery) stream.fromXML( xmlRequest );
  31. List<Item> itens = retorno.getResults().getItens();
  32. for (Item item : itens) {
  33. System.out.println( " -----/----- " );
  34. System.out.println( "TITLE: " + item.getTitle() );
  35. System.out.println( "LINK: " + item.getLink() );
  36. for (String categoria : item.getCategoryList())
  37. System.out.println( " CATEGORY: " + categoria );
  38. }
  39. }
  40. private static YQLQuery createQuery() {
  41. Item item1 = new Item();
  42. item1.setTitle("titulo 1");
  43. Item item2 = new Item();
  44. item2.setTitle("titulo 2");
  45. Results results = new Results();
  46. results.addItem( item1 );
  47. results.addItem( item2 );
  48. YQLQuery query = new YQLQuery();
  49. query.setResults( results );
  50. XStream stream = new XStream();
  51. stream.autodetectAnnotations(true);
  52. String xml = stream.toXML( query );
  53. System.out.println( xml );
  54. return query;
  55. }
  56. private static String httpRequest(String url) throws IOException{
  57. URL theUrl = new URL( url );
  58. URLConnection yc = theUrl.openConnection();
  59. BufferedReader in = new BufferedReader(
  60. new InputStreamReader(
  61. yc.getInputStream())
  62. );
  63. StringBuffer sb = new StringBuffer();
  64. String inputLine;
  65. while ((inputLine = in.readLine()) != null)
  66. sb.append( inputLine );
  67. in.close();
  68. return sb.toString();
  69. }
  70. }