BOILING OUT BOILERPLATE


Hi, I'm Maciej


BOILERPLATE

NETWORKING

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");
}

NETWORKING

    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);
}
}

NETWORKING

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;


Networking

  • Retrofit
  • Picasso
  • Universal Image Loader


NETWORKING

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();


LAZY, But...

Intent intent = new Intent(this, MenuItemActivity.class);
intent.putExtra(MenuItemActivity.EXTRA_PLACE, currentPlace);
intent.putExtra(MenuItemActivity.EXTRA_ITEM, selectedItem);
startActivity(intent);


LAZY, But...

MenuItemActivity.intent(this)
.place(currentPlace)
.item(selectedItem)
.start();



HOW?


    REFLECTION

    • RoboGuice


    ANNOTATION PROCESSING API

    • Android Annotations
    • ButterKnife
    • Parceler
    • ...


    BYTECODE WEAVING

    • Hugo
    • LogLifeCycle


    AST TRANSFORMATIONS

    • Lombok + Hrisey
    • Groovy


    TALK IS CHEAP,

    SHOW ME THE CODE


    Q || A


    THANKS!


    LINKS

    Made with Slides.com