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");
JSONObject dayForecast, weatherObject;
String[] resultStrs = new String[numDays];
for(int i = 0; i < weatherArray.length(); i++) {
dayForecast = weatherArray.getJSONObject(i);
long dateTime = dayForecast.getLong("dt");
String day = getReadableDateString(dateTime);
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");
public interface WeatherApi {
@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();
public final class LoginActivity extends Activity {
@MagicViews
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
usernameField.setText(prefs.getUsername());
}
private void onSignInButtonClick() {
String username = usernameField.getText().toString();
String password = passwordField.getText().toString();
// make API call
}
}
Now code only what is worth coding!