Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. public class app
  2.     {
  3.     private static String urlString;
  4.     private static URL url = null;
  5.     public static void main(String[] args) throws IOException  
  6.         {
  7.                 String urlStr= "http://api.worldweatheronline.com/premium/v1/weather.ashx?key=c6af9bbd12814081994190330191301&q=";
  8.                 String city =  args[0];
  9.                 String days = args[1];
  10.                 urlString = urlStr + URLEncoder.encode(city, "UTF8")+"&format=json&num_of_days=" + URLEncoder.encode(days, "UTF8");        
  11.                 BufferedReader reader= null;
  12.                 HttpURLConnection connection = null;
  13.                 StringBuilder stringBuilder = new StringBuilder();
  14.                 String line =null;
  15.                 try
  16.                 {
  17.                     url = new URL(urlString);
  18.                 }
  19.                 catch(MalformedURLException e)
  20.                 {
  21.                     System.out.println("URL Error");
  22.                 }
  23.                 try
  24.                 {
  25.                     connection = (HttpURLConnection)url.openConnection();
  26.                     connection.setRequestMethod("GET");
  27.                     connection.setReadTimeout(15*100000);
  28.                     connection.connect();
  29.                 }
  30.                 catch(NullPointerException e)
  31.                 {
  32.                     System.out.println("Connection error");
  33.                 }
  34.                 try
  35.                 {
  36.                     reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  37.                     while((line = reader.readLine()) != null)
  38.                     {
  39.                         stringBuilder.append(line);
  40.                     }
  41.                 }
  42.                 catch(NullPointerException e)
  43.                 {
  44.                     System.out.println("Reader error");
  45.                 }
  46.                 String jsonString = stringBuilder.toString();          
  47.                 if(reader!=null)
  48.                 reader.close();
  49.                 try
  50.                 {
  51.                     JsonObject jsonObj = new JsonParser().parse(jsonString).getAsJsonObject();
  52.                     JsonObject jsonData = jsonObj.getAsJsonObject("data");
  53.                     JsonArray jsonArray = jsonData.getAsJsonArray("request");
  54.                     for(int i = 0;i<jsonArray.size();i++)
  55.                     {
  56.                         String postId = jsonArray.get(i).getAsJsonObject().get("query").getAsString();
  57.                         System.out.println(postId);
  58.                     }
  59.                     jsonArray = jsonData.getAsJsonArray("weather");
  60.                     for(int i = 0;i<jsonArray.size();i++)
  61.                     {
  62.                         String postId = jsonArray.get(i).getAsJsonObject().get("date").getAsString();
  63.                         String postId2 = jsonArray.get(i).getAsJsonObject().get("mintempC").getAsString();
  64.                         String postId3 = jsonArray.get(i).getAsJsonObject().get("maxtempC").getAsString();
  65.                         System.out.println(postId + " temperatura min = " + postId2 + ", temperatura max = " + postId3);
  66.                     }
  67.                 }
  68.                 catch(IllegalStateException e)
  69.                 {
  70.                     System.out.println("Json object error");
  71.                 }
  72.         }          
  73.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement