Android无法解析rxjava中的订阅方法

Hus*_*ghi 1 android rx-android retrofit2 rx-java2

我打算在Android Studio中使用rxjava进行改造。实际上我遵循了 https://code.tutsplus.com/tutorials/getting-started-with-retrofit-2--cms-27792教程这就是我所做的一切:

compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'com.squareup.retrofit2:retrofit:2.2.0'
    compile 'com.google.code.gson:gson:2.8.0'
    compile 'com.squareup.retrofit2:converter-gson:2.2.0'
    compile 'com.android.support:recyclerview-v7:25.1.0'
    compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
    compile 'io.reactivex.rxjava2:rxjava:2.0.9'
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
Run Code Online (Sandbox Code Playgroud)

我创建了Retrofit客户端和Interface订阅方法。说不能解决

mService.getAnswer().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
             .subscribe(new Subscriber<SOAnswersResponse>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(SOAnswersResponse soAnswersResponse) {

                            }


       }
         );
Run Code Online (Sandbox Code Playgroud)

我在主要活动中导入了此内容:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import android.widget.Toast;

import com.example.android.stackoverflow.Data.Model.Item;

import com.example.android.stackoverflow.Data.Model.SOAnswersResponse;
import com.example.android.stackoverflow.Data.remote.ApiUtils;
import com.example.android.stackoverflow.Data.remote.SOService;


import java.util.ArrayList;


import io.reactivex.schedulers.Schedulers;
import io.reactivex.android.schedulers.AndroidSchedulers;
import rx.Subscriber;
Run Code Online (Sandbox Code Playgroud)

Tom*_*iak 6

Rxplanation

在教程“您已链接”中使用了RxJava1。在RxJava 2中,方法subscribe不接受Subscriber类实例作为参数。您必须使用:

上面提到的所有运算符也都具有subscribe()不带参数的方法。

清理工作

您的依赖项中有些混乱。我能想到的最简短的依赖项列表是:

// Reactive extensions.
compile 'io.reactivex.rxjava2:rxjava:2.1.0'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'

// Networking.
compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:converter-jackson:2.2.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
Run Code Online (Sandbox Code Playgroud)

特别是,请不要使用RxAndroid已完成的两个不同版本的或适配器:

compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
Run Code Online (Sandbox Code Playgroud)

compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
Run Code Online (Sandbox Code Playgroud)

也已不再使用Jake Wharton的适配器。Square已为RxJava2准备了适配器:

compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
Run Code Online (Sandbox Code Playgroud)

样例代码

现在,配置完所有内容后,域模型可以如下所示:

package com.todev.rxretrofit;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;

import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(value = NON_NULL)
class Song {

  private String id;

  private String title;

  public String getId() {
    return id;
  }

  public String getTitle() {
    return title;
  }
}
Run Code Online (Sandbox Code Playgroud)

请记住,我使用了Jackson(因为使用IMHO更简单)。不要犹豫,使用GSON批注,但是您将不得不将Jackson依赖项更改为GSON并重新配置Retrofit服务实例构造。

简单的服务界面:

package com.todev.rxretrofit;

import io.reactivex.Single;
import java.util.Collection;
import retrofit2.http.GET;

interface CustomService {

  @GET("songs")
  Single<Collection<Song>> getAllSongs();
}
Run Code Online (Sandbox Code Playgroud)

最后(但并非最不重要)在活动中的用法(我故意包括所有进口):

package com.todev.rxretrofit;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import java.util.Collection;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;

public class MainActivity extends AppCompatActivity {

  private CustomService customService = new Retrofit.Builder()
      .baseUrl("http://<api_address>:<api_port>/")
      .addConverterFactory(JacksonConverterFactory.create())
      .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
      .client(new OkHttpClient.Builder().build())
      .build()
      .create(CustomService.class);

  private CompositeDisposable disposables = new CompositeDisposable();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.disposables.add(
        this.customService.getAllSongs()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(this.responseHandler, this.errorHandler));
  }

  @Override
  protected void onDestroy() {
    this.disposables.dispose();
    super.onDestroy();
  }

  private Consumer<Collection<Song>> responseHandler = new Consumer<Collection<Song>>() {
    @Override
    public void accept(Collection<Song> songs) throws Exception {
      // TODO: Handle response.
      for (Song song : songs) {
        Log.d(this.getClass().getSimpleName(), String.valueOf(song));
      }
    }
  };

  private Consumer<Throwable> errorHandler = new Consumer<Throwable>() {
    @Override
    public void accept(@NonNull Throwable throwable) throws Exception {
      // TODO: Handle error.
      Log.d(this.getClass().getSimpleName(), throwable.getLocalizedMessage());
    }
  };
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我使用了Consumer。您也可以使用lambda表达式,而使用类方法。

在此处输入图片说明

注意

此示例中使用的JSON文档由json-server通过简单文本文件提供:

[
  {
    "id": 0,
    "title": "Song of Fire and Ice"  
  },
  {
    "id": 1,
    "title": "The Hanging Tree"
  }
]
Run Code Online (Sandbox Code Playgroud)

投稿后

请记住在清单中添加Internet权限:

<uses-permission android:name="android.permission.INTERNET"/>
Run Code Online (Sandbox Code Playgroud)

否则,您将收到以下SocketException原因:

android.system.ErrnoException:套接字失败:EACCES(权限被拒绝)