use of rx.functions.Func2 in project AnDevCon-RxPatterns by colintheshots.
the class Example7 method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example7);
mTextView = (TextView) findViewById(R.id.example7_textView);
mTextView2 = (TextView) findViewById(R.id.example7_textView2);
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(loggingInterceptor).build();
mWeatherInterface = new Retrofit.Builder().baseUrl("http://api.openweathermap.org").client(client).addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io())).addConverterFactory(GsonConverterFactory.create(gson)).build().create(WeatherInterface.class);
// Since we're returning a basic type from Observable.zip()
// instead of an observable of a type, we can simply map to
// the new type. In fact, the cast() operator is probably
// good enough here.
Observable.zip(mIntegerObservable1, mIntegerObservable2, new Func2<Integer, Integer, Integer>() {
@Override
public Integer call(Integer integer, Integer integer2) {
return integer + integer2;
}
}).map(new Func1<Integer, String>() {
@Override
public String call(Integer integer) {
return Integer.toString(integer);
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<String>() {
@Override
public void call(String s) {
mTextView.setText(s);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace();
}
});
Observable.zip(mObservable1, mObservable2, new Func2<String, String, Observable<CityWeather>>() {
@Override
public Observable<CityWeather> call(String lat, String lon) {
return mWeatherInterface.getWeather(lat, lon);
}
}).flatMap(new Func1<Observable<CityWeather>, Observable<CityWeather>>() {
@Override
public Observable<CityWeather> call(Observable<CityWeather> cityWeatherObservable) {
return cityWeatherObservable;
}
}).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<CityWeather>() {
@Override
public void call(CityWeather cityWeather) {
mTextView2.setText("City: " + cityWeather.name + "\nTemp: " + Float.toString(cityWeather.main.temp) + " Kelvins");
}
});
}
Aggregations