HttpURLConnection urlConnection = null;BufferedReader reader = null;String forecastJsonStr = null; try { URL url = new URL("http://api.openweathermap.org/data/2.5/forecast /daily?q=94043&mode=json&units=metric&cnt=7");urlConnection = (HttpURLConnection) url.openConnection();urlConnection.setRequestMethod("GET");urlConnection.connect();InputStream inputStream = urlConnection.getInputStream();StringBuffer buffer = new StringBuffer();if (inputStream == null) {forecastJsonStr = null;}reader = new BufferedReader(new InputStreamReader(inputStream));String line;while ((line = reader.readLine()) != null) {buffer.append(line + "\n");}
if (buffer.length() == 0) {forecastJsonStr = null;}forecastJsonStr = buffer.toString();} catch (IOException e) {Log.e("PlaceholderFragment", "Error ", e);forecastJsonStr = null;} finally{if (urlConnection != null) {urlConnection.disconnect();}if (reader != null) {try {reader.close();} catch (final IOException e) {Log.e("PlaceholderFragment", "Error closing stream", e);}}
JSONObject forecastJson = new JSONObject(forecastJsonStr);JSONArray weatherArray = forecastJson.getJSONArray("list");String[] resultStrs = new String[numDays];for(int i = 0; i < weatherArray.length(); i++) {JSONObject dayForecast = weatherArray.getJSONObject(i);long dateTime = dayForecast.getLong("dt");String day = getReadableDateString(dateTime);JSONObject weatherObject = dayForecast.getJSONArray("weather").getJSONObject(0);String description = weatherObject.getString("main");JSONObject temperatureObject = dayForecast.getJSONObject("temp");double high = temperatureObject.getDouble("max");double low = temperatureObject.getDouble("min");String highAndLow = formatHighLows(high, low);resultStrs[i] = day + " - " + description + " - " + highAndLow;}return resultStrs;
public interface GithubApi {
@GET("/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7")
List<WeatherResult> requestWeatherForNext7Days();
}
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://api.openweathermap.org")
.build();
WeatherApi api = restAdapter.create(WeatherApi.class);
List<WeatherResult> results = api.requestWeatherForNext7Days();
Intent intent = new Intent(this, MenuItemActivity.class);
intent.putExtra(MenuItemActivity.EXTRA_PLACE, currentPlace);
intent.putExtra(MenuItemActivity.EXTRA_ITEM, selectedItem);
startActivity(intent);
MenuItemActivity.intent(this)
.place(currentPlace)
.item(selectedItem)
.start();