EventBus结合rxjava2和retrofit2网络获取

时间:2023-03-09 04:37:41
EventBus结合rxjava2和retrofit2网络获取

依赖:

compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'io.reactivex.rxjava2:rxjava:2.1.3'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1' implementation 'org.greenrobot:eventbus:3.1.1'
网络权限:
<uses-permission android:name="android.permission.INTERNET" />
代码实现:
布局:
<cn.bingoogolapple.refreshlayout.BGARefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bga_rl"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.think.bga.BGARefreshLayout0Activity"> <android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
</android.support.v7.widget.RecyclerView> </cn.bingoogolapple.refreshlayout.BGARefreshLayout>
Activity:
public class BGARefreshLayout4Activity extends AppCompatActivity {

    private RecyclerView rv;
private OkHttpClient client;
private RvAdapter adapter;
private String baseUrl = "http://www.qubaobei.com/";
private String urlAll = "http://www.qubaobei.com/ios/cf/dish_list.php/?stage_id=1&limit=10&page=1"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bgarefresh_layout0);
//第一步1,注册.
EventBus.getDefault().register(this);
client = new OkHttpClient
.Builder()
.build();
initView();
request();
} private void request() {
Observable.create(new ObservableOnSubscribe<FoodInfo>() {
@Override
public void subscribe(ObservableEmitter<FoodInfo> e) throws Exception {
//发射数据的逻辑.
Response<FoodInfo> builder = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
.create(NetInterface.class)
.getDataFromNet2(urlAll)
.execute();
FoodInfo body = builder.body();
//第三步,发送事件.
EventBus.getDefault().post(body); }
}).subscribeOn(Schedulers.io())//开启子线程
.subscribe();
} private void initView() {
rv = (RecyclerView) findViewById(R.id.rv); adapter = new RvAdapter(rv);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setAdapter(adapter); } //第二步,订阅事件,接收数据.
@Subscribe(threadMode = ThreadMode.MAIN)
public void reciveResult(FoodInfo result) {
adapter.setData(result.getData());
} @Override
protected void onDestroy() {
super.onDestroy();
//第一步2,注销.
EventBus.getDefault().unregister(this);
}
}

  子布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="horizontal"> <ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" /> <LinearLayout
android:layout_marginLeft="20dp"
android:layout_gravity="center_vertical"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"> <TextView
android:text="11111111"
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="2222222"
android:id="@+id/tv_food"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout> </LinearLayout>

适配器:

public class RvAdapter extends BGARecyclerViewAdapter<FoodInfo.DataBean> {

    public RvAdapter(RecyclerView recyclerView) {
super(recyclerView,R.layout.item_rv);
} @Override
protected void fillData(BGAViewHolderHelper helper, int position, FoodInfo.DataBean model) {
helper.setText(R.id.tv,model.getTitle())
.setText(R.id.tv_food,model.getFood_str())
.setImageResource(R.id.iv,R.mipmap.ic_launcher_round);
} }