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


NETWORKING

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


NETWORKING

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


Networking

  • Retrofit
  • Picasso
  • Universal Image Loader


NETWORKING

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


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 / AutoParcel
    • Icepick / Memento
    • ...


    BYTECODE WEAVING

    • Hugo
    • LogLifeCycle


    AST TRANSFORMATIONS

    • Lombok + Hrisey
    • Groovy


    MAGIC?

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


    TALK IS CHEAP,

    SHOW ME THE CODE


    Q || A


    THANKS!

    Now code only what is worth coding!


    LINKS

    Boiling out Boilerplate / Mobilization 2014

    By Maciej Górski

    Boiling out Boilerplate / Mobilization 2014

    • 2,168