如何在android架构组件中使用RxJava而不是LiveData?

如何在android架构组件中使用RxJava而不是LiveData?,android,rx-android,android-architecture-components,Android,Rx Android,Android Architecture Components,注意:如果您已经在使用RxJava或AGRA之类的库,您可以 继续使用它们而不是LiveData。但是当你使用它们或 其他方法,请确保正确处理生命周期 这样,当相关LifecycleOwner 当LifecycleOwner停止时,流将被销毁 摧毁。您还可以添加android.arch.lifecycle:reactivestreams 将LiveData与另一个反应流库一起使用的工件(用于 例如,RxJava2) 上面的语句是从android开发者页面复制而来的。在这里,它指定如果你使用RxJ

注意:如果您已经在使用RxJava或AGRA之类的库,您可以 继续使用它们而不是LiveData。但是当你使用它们或 其他方法,请确保正确处理生命周期 这样,当相关LifecycleOwner 当LifecycleOwner停止时,流将被销毁 摧毁。您还可以添加android.arch.lifecycle:reactivestreams 将LiveData与另一个反应流库一起使用的工件(用于 例如,RxJava2)

上面的语句是从android开发者页面复制而来的。在这里,它指定如果你使用RxJava,你就不需要使用LiveData。两者都遵循可观察的模式。我想知道如何使用RxJava而不是带有rest api调用的LiveData。我试了很多次,但找不到任何答案。如果有人帮助我解决这个问题,那将非常有用。提前感谢

请看


RxJava的可流动实现
Publisher
接口。您可以将
LiveData
转换为
Flowable
并从那里开始。

您可以使用这些方法将
Publisher
转换为
LiveData
,反之亦然

  • LiveDataReactiveStreams.fromPublisher(Publisher-Publisher)
    --将为您提供
    LiveData
    对象

  • LiveDataReactiveStreams.TopPublisher(LifecycleOwner lifecycle,LiveData LiveData)
    --将为您的
    RxJava
    发布者提供对象

然后您可以使用
pub-sub
模式,只需
订阅发布者


这是一个简单的示例,在这个示例上,使用RX的改装解析GET响应

假设响应

   {
            "id": "0001",
                "type": "donut",
                "name": "Cake",
                "image": {
            "url": "images/0001.jpg",
                    "width": 200,
                    "height": 200
        },
            "thumbnail": {
            "url": "images/thumbnails/0001.jpg",
                    "width": 32,
                    "height": 32
        }
 @GET("nestedjson5")
    Observable<NestedJson5Main> nestedJson5();
public class Image {

    String url;
    int width;
    int height;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}
ApiInterface

   {
            "id": "0001",
                "type": "donut",
                "name": "Cake",
                "image": {
            "url": "images/0001.jpg",
                    "width": 200,
                    "height": 200
        },
            "thumbnail": {
            "url": "images/thumbnails/0001.jpg",
                    "width": 32,
                    "height": 32
        }
 @GET("nestedjson5")
    Observable<NestedJson5Main> nestedJson5();
public class Image {

    String url;
    int width;
    int height;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}
模型类

public class NestedJson5Main {

    String id;
    String type;
    String name;
    Image image;
    Image thumbnail;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

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

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    public Image getThumbnail() {
        return thumbnail;
    }

    public void setThumbnail(Image thumbnail) {
        this.thumbnail = thumbnail;
    }
}
public class ParsingVm extends BaseObservable {

    Context context;

    ApiInterface mApiInterface;

    public ParsingVm(Context context) {

        this.context = context;
        mApiInterface = AppController.getInstance().getmNetComponent().getApiInterface();

    }

    public Observable<NestedJson5Main> nestedJson5Main() {

        return AppController.getInstance().getmNetComponent().getApiInterface()
                .nestedJson5()
                .subscribeOn(Schedulers.newThread());
    }
    }
视图模型类

public class NestedJson5Main {

    String id;
    String type;
    String name;
    Image image;
    Image thumbnail;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

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

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    public Image getThumbnail() {
        return thumbnail;
    }

    public void setThumbnail(Image thumbnail) {
        this.thumbnail = thumbnail;
    }
}
public class ParsingVm extends BaseObservable {

    Context context;

    ApiInterface mApiInterface;

    public ParsingVm(Context context) {

        this.context = context;
        mApiInterface = AppController.getInstance().getmNetComponent().getApiInterface();

    }

    public Observable<NestedJson5Main> nestedJson5Main() {

        return AppController.getInstance().getmNetComponent().getApiInterface()
                .nestedJson5()
                .subscribeOn(Schedulers.newThread());
    }
    }

我添加了一个简单的答案,在这个答案上创建了可观察对象,然后进行了观察。

我用网络调用更新了答案。请看this@keerthiprasad好的,谢谢。我会调查的