Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. String startWebRequest(String city) throws IOException
  2. {
  3. String weatherURL = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&APPID=9b3f816b60d19f5e2f8b65fca37e91fb"; //please get your own token to use from API.Openweathermap
  4.  
  5. StringBuilder result = new StringBuilder(); //this is going to hold the JSON Response from the server
  6. URL url = new URL(weatherURL);
  7. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  8. conn.setRequestMethod("GET");
  9. BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  10. String line;
  11. while ((line = rd.readLine()) != null) {
  12. result.append(line);
  13. }
  14. rd.close();
  15.  
  16. double minTemp = parseMinTemp(result.toString());
  17. double maxTemp = parseMaxTemp(result.toString());
  18. double curTemp = parseCurTemp(result.toString());
  19. DecimalFormat df = new DecimalFormat("#.##");
  20. String out = "Current Temperature: " + df.format(curTemp) + "\u00b0F Minimum Temperature: " + df.format(minTemp) + "\u00b0F Maximum Temperature: " +df.format(maxTemp) + "\u00b0F";
  21.  
  22. return out;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement