Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String startWebRequest(String city) throws IOException
- {
- String weatherURL = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&APPID=9b3f816b60d19f5e2f8b65fca37e91fb"; //please get your own token to use from API.Openweathermap
- StringBuilder result = new StringBuilder(); //this is going to hold the JSON Response from the server
- URL url = new URL(weatherURL);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
- BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- String line;
- while ((line = rd.readLine()) != null) {
- result.append(line);
- }
- rd.close();
- double minTemp = parseMinTemp(result.toString());
- double maxTemp = parseMaxTemp(result.toString());
- double curTemp = parseCurTemp(result.toString());
- DecimalFormat df = new DecimalFormat("#.##");
- String out = "Current Temperature: " + df.format(curTemp) + "\u00b0F Minimum Temperature: " + df.format(minTemp) + "\u00b0F Maximum Temperature: " +df.format(maxTemp) + "\u00b0F";
- return out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement