RxJava和Retrofit结合了拉链调用

时间:2017-01-31 13:05:13

标签: java android retrofit rx-java rx-android

Heye stackoverflow, 我正在考虑将RxJava和Retrofit结合在一起,但我遇到了一个小问题。

我得到的代码只能进行一次网络通话。现在我正在尝试将调用与Observable.zip方法结合起来。我的代码中没有错误但是从不调用响应方法。你们中的任何人都知道代码有什么问题:

MainActivity

public class MainActivity extends AppCompatActivity {

public static final String BASE_URL = "https://api.learn2crack.com/";

private RecyclerView mRecyclerView;

private CompositeDisposable mCompositeDisposable;

private DataAdapter mAdapter;

private ArrayList<Android> mAndroidArrayList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mCompositeDisposable = new CompositeDisposable();
    initRecyclerView();
    loadJSON();
}

private void initRecyclerView() {
    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    mRecyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
    mRecyclerView.setLayoutManager(layoutManager);
}

private void loadJSON() {

    RequestInterface requestInterface = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build().create(RequestInterface.class);

     //        //Single request
     //        mCompositeDisposable.add(requestInterface.register()
     //                .observeOn(AndroidSchedulers.mainThread())
     //                .subscribeOn(Schedulers.io())
     //                .subscribe(this::handleResponse, this::handleError));

    //double request
    Observable<List<Android>> listObservable = requestInterface.register()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread());
    Observable<List<Android>> listObservable2 = requestInterface.register()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread());

    Observable.zip(listObservable, listObservable2, CombinedAndroid::new)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(this::handleResponse2,this::handleError);

}

private void handleResponse(List<Android> androidList) {
    mAndroidArrayList = new ArrayList<>(androidList);
    mAdapter = new DataAdapter(mAndroidArrayList);
    mRecyclerView.setAdapter(mAdapter);
}

private void handleResponse2(CombinedAndroid combinedAndroid) {
    mAndroidArrayList = new ArrayList<>(combinedAndroid.getAndroid());
    mAdapter = new DataAdapter(mAndroidArrayList);
    mRecyclerView.setAdapter(mAdapter);
}

private void handleError(Throwable error) {
    Toast.makeText(this, "Error " + error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mCompositeDisposable.clear();
}
}

Android模型

public class Android {

private String ver;
private String name;
private String api;

public String getVer() {
    return ver;
}

public void setVer(String ver) {
    this.ver = ver;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getApi() {
    return api;
}

public void setApi(String api) {
    this.api = api;
}
}

CombinedAndroid模型

public class CombinedAndroid {
private List<Android> android;
private List<Android> android2;

public CombinedAndroid(List<Android> android, List<Android> android2) {
    this.android = android;
    this.android2 = android2;
}

public List<Android> getAndroid() {
    return android;
}

public void setAndroid(List<Android> android) {
    this.android = android;
}

public List<Android> getAndroid2() {
    return android2;
}

public void setAndroid2(List<Android> android2) {
    this.android2 = android2;
}
}

RequestInterface

public interface RequestInterface {

@GET("android/jsonarray/")
Observable<List<Android>> register();
}

适配器

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder>{

private ArrayList<Android> mAndroidList;

public DataAdapter(ArrayList<Android> androidList){
    mAndroidList = androidList;
}

class ViewHolder extends RecyclerView.ViewHolder{

    private TextView mName, mVersion, mApi;

    ViewHolder(View itemView) {
        super(itemView);
        mName = (TextView) itemView.findViewById(R.id.tv_name);
        mVersion = (TextView) itemView.findViewById(R.id.tv_version);
        mApi = (TextView) itemView.findViewById(R.id.tv_api_level);
    }
}

@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_row, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(DataAdapter.ViewHolder holder, int position) {
    holder.mName.setText(mAndroidList.get(position).getName());
    holder.mVersion.setText(mAndroidList.get(position).getVer());
    holder.mApi.setText(mAndroidList.get(position).getApi());
}

@Override
public int getItemCount() {
    return mAndroidList != null ? mAndroidList.size() : 0;
}
}

0 个答案:

没有答案