Open wangshengwu-source opened 7 years ago
你检查一下导包是不是导成了数据库的包而不是Rxjava的
非常感谢您的及时回复;我刚刚看到,不好意思:) 情况是这样的,我直接下载您的源码,然后放在as中,在javaBean/HttpMethods.java中的getJoke方法中加入了
.map(new Function<List, Object>() { //add
@Override
public Object apply(@NonNull List myJokes) throws Exception {
return myJokes;
}
})
一段代码,其他没有改变,如图: 添加后,在编辑器,导入响应包 但是到了最后一行,无法通过
该文件中,所有引用包如下:
package com.example.nanke.joke;
import com.example.nanke.joke.JavaBean.MyJoke;
import java.util.List;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
app下的build.gradle没有修改,仍是原来的。
用map的话得将observer进行强转
我这里强转后,编译通过,但是运行崩溃,报:
FATAL EXCEPTION: main
Process: com.example.nanke.joke, PID: 3093
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nanke.joke/com.example.nanke.joke.MainActivity}: java.lang.ClassCastException: com.example.nanke.joke.MainActivity$2 cannot be cast to io.reactivex.functions.Consumer
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.ClassCastException: com.example.nanke.joke.MainActivity$2 cannot be cast to io.reactivex.functions.Consumer
at com.example.nanke.joke.HttpMethods.getJoke(HttpMethods.java:65)
at com.example.nanke.joke.MainActivity.getData(MainActivity.java:59)
at com.example.nanke.joke.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
您那边可以正常运行吗?
我这里简单的加了一个map操作符,原样返回List参数,但是最后的subscribe却报错了,
刚刚接触rxjava,不知道是不是我哪里写错了。
apiService.getData() .map(new Function<List, Object>() { //add
@Override
public Object apply(@NonNull List myJokes) throws Exception {
return myJokes;
}
})
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer); //报Cannot Resolve method Subscribe