我在我的项目中使用Rxjava和Retrofit2.0,它看起来像这样:
java.io.BufferedOutputStream
如果我不使用反应方式,它将是这样的:
<?php
/**
* Example Widget Class
*/
class dazzling_whats_trending extends WP_Widget {
/** constructor -- name this the same as the class above */
function dazzling_whats_trending() {
parent::WP_Widget(false, $name = 'Dazzling Whats Trending Widget');
}
/** @see WP_Widget::widget -- do not rename this */
function widget($args, $instance) {
extract( $args );
$number = apply_filters('number', $instance['number']);
$offset = $instance['offset'];
?>
<div id="popular-posts" class="sidebar_content tab-pane active">
<?php
$recent_posts = new WP_Query( array(
'showposts' => $number,
'ignore_sticky_posts' => 1,
'post_status' => 'publish',
'order' => 'DESC',
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'offset' => $offset
) );
?>
<?php while($recent_posts->have_posts()): $recent_posts->the_post(); ?>
<li>
<?php the_post_thumbnail( array(336,210) ); ?>
<h4 class="blacktext"><a href="<?php the_permalink(); ?>"><?php echo the_title(); ?></a></h4>
<span class="orangetext">
<?php
$post = get_post();
$views = get_post_meta($post->ID, 'post_views_count', true);
if($views > 1000){
$views_count=$views *1/1000;
$views_k=round($views_count,PHP_ROUND_HALF_UP);
echo $views_k.'K';
}
else {
echo $views;
}
?> VIEWS
</span>
<span class="graytext">|
<?php
$category = get_the_category();
$category_parent_id = $category[0]->category_parent;
$category_link = get_category_link($category_parent_id);
if ( $category_parent_id != 0 ) {
$category_link = get_category_link($category_parent_id);
$category_parent = get_term( $category_parent_id, 'category' );
$catname = $category_parent->name;
} else {
$category_link = get_category_link($category_id);
$catname = $category[0]->name;
}
?>
<a href="<?php echo esc_url($category_link); ?>"><?php echo $catname; ?></a>
</span>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</div>
<?php
}
/** @see WP_Widget::update -- do not rename this */
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['number'] = strip_tags($new_instance['number']);
$instance['offset'] = strip_tags($new_instance['offset']);
return $instance;
}
/** @see WP_Widget::form -- do not rename this */
function form($instance) {
$number = esc_attr($instance['number']);
$offset = esc_attr($instance['offset']);
?>
<p>
<label for="<?php echo $this->get_field_id('offset'); ?>"><?php _e('Offset number of posts by'); ?></label>
<input size="3" id="<?php echo $this->get_field_id('offset'); ?>" name="<?php echo $this->get_field_name('offset'); ?>" type="text" value="<?php echo $offset; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label>
<input size="3" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" />
</p>
<?php
}
} // end class example_widget
add_action('widgets_init', create_function('', 'return register_widget("dazzling_whats_trending");'));
?>
所以,我的问题是如何使用Rxjava来实现这个以及使用什么运算符?
答案 0 :(得分:8)
使用Rx,您最终可以不再考虑“嵌套”网络请求。
如果你考虑一下,你真的不想“嵌套”网络请求,对吧?这只是代码最终看起来的样子,因为您只有AsyncTask
或其他回调。
使用Rx,您最终可以编写在其结构中类似于您实际想要的代码,即链网络请求:首先做一件事,然后做另一个。
getAFromServer()
.flatMap(new Func1<List<A>, Observable<A>>() {
@Override
public Observable<A> call(List<A> list) {
// the first flatMap is just a trick to "flatten" the Observable
return Observable.from(list);
}
})
.flatMap(new Func1<A, Observable<B>>() {
@Override
public Observable<A> call(A someA) {
return getBFromServer(someA.a);
}
})
.subscribe(...);