Unable to create call adapter for io.reactivex.Single - android

I want to conncet to server with retrofit and rxjava.it works when I used call and everything is good.but when try to use rxjava ,its getsinto trouble.
the error text:
Could not locate call adapter for io.reactivex.Single
in the build.gradle I implemented the retrofit adapter.but I dont know whats the problem.
this is my gradle:
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.8'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation "android.arch.persistence.room:runtime:1.1.1"
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"
api client code:
public class ApiClient {
public static final String BASE_URL="http://192.168.1.100/digikala/";
private static Retrofit retrofit=null;
public static Retrofit getClient(){
if(retrofit==null){
retrofit=new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(BASE_URL)
.build();
}
return retrofit;
}
api service code:
public interface ApiService {
#GET("readamazing.php")
Single<List<Product>> getSingleProducts();
}
main acitivity code:
ApiService apiService=ApiClient.getClient().create(ApiService.class);
apiService.getSingleProducts().subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<List<Product>>() {
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onSuccess(List<Product> products) {
Log.i("LOG", "onSuccess: "+products.toString());
}
#Override
public void onError(Throwable e) {
Log.i("LOG", "onSuccess: "+e.toString());
}
});

Use RxJava2CallAdapterFactory instead of RxJavaCallAdapterFactory as RxJava2 is used.
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
to the Retrofit.Builder().

Related

Retrofit is not making api call

I'm using retrofit 2 to make api call to my server but it get stucked when trying to make api call. This is my code
public interface GOTApi {
#GET("characters.json")
Call<GOTCharacterResponse> getCharacters();
}
Intermediate class to get the data
public class GOTCharacterResponse {
List<GOTCharacter> characters;
}
My class to make api call
public class GOTService {
public static final String BASE_URL = "https://project-8424324399725905479.firebaseio.com/";
public static GOTApi getGOTApi(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit.create(GOTApi.class);
}
public static void getCharacters(){
getGOTApi().getCharacters().enqueue(new Callback<GOTCharacterResponse>() {
#Override
public void onResponse(Call<GOTCharacterResponse> call, Response<GOTCharacterResponse> response) {
if(response.isSuccessful()){
}
}
#Override
public void onFailure(Call<GOTCharacterResponse> call, Throwable t) {
int a = 0;
}
});
}
}
These are the libraries I'm using
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.okhttp3:okhttp:3.3.1'
It always get stucked in the getCharacters() method. Of course I have internet permission set in Mainfest.
You may try using Retrofit2 with RxJava, it is more convenient.
public Retrofit providedRetrofit(OkHttpClient okHttpClient){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
return retrofit;
}
Your API interface will look like
public interface Api {
#GET("api/service/schedule/{filial}")
Observable<Response<GOTCharacter>> getSchedule(#Path("some_param") String param);
}
You also need to parse response from JSON. You didn't provided
GOTCharacter class, but you can create code from json response by using
http://www.jsonschema2pojo.org/ service
I think you are implementing wrong onResponse() OR Callback(), because I am using Retrofit 2 too, in which onResponse() looks like this:
#Override
public void onResponse(Response<ListJsonResponseRestaurant> response, Retrofit retrofit) {
...
...
}

Json Object Request using Retrofit2

I am new in retrofit. I completed all setup.
I add this gradle in build.gradle file
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
My Interface is like this:
public interface ILoginInterface {
String BASE_URL= "MY_BASE_URL/";
#POST("MY/API")
Call<LoginResponseEntity> startLogin(#Body JSONObject jsonObject);
class Factory{
private static ILoginInterface instance;
public static ILoginInterface getInstance(){
if(instance==null){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
instance = retrofit.create(ILoginInterface.class);
}
return instance;
}
}
}
My Calling procedure is like this:
ILoginInterface.Factory.getInstance().startLogin(jsonObject).enqueue(new Callback<LoginResponseEntity>() {
#Override
public void onResponse(Call<LoginResponseEntity> call, retrofit2.Response<LoginResponseEntity> response) {
Log.d("MS",response.body().fullName);
}
#Override
public void onFailure(Call<LoginResponseEntity> call, Throwable t) {
Log.d("MS",t.getMessage());
}
});
Here jsonObject is like this:
{"user_name":"sajedul Karim", "password":"123456"}
Here it seems everything is ok but i didn't getting proper response.
I found a solution. it is here . Does anybody have proper solution like Volley JsonObjectRequest
May be you are importing
import org.json.JSONObject;
you should use
import com.google.gson.JsonObject;
Then you will get it's value.

IllegalArgumentException: Could not locate call adapter for rx.Observable RxJava, Retrofit2

I am getting the above error while calling the rest api. I am using both retrofit2 and RxJava.
ServiceFactory.java
public class ServiceFactory {
public static <T> T createRetrofitService(final Class<T> clazz, final String endpoint){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(endpoint)
//.addConverterFactory(GsonConverterFactory.create())
.build();
T service = retrofit.create(clazz);
return service;
}
}
MovieService.java
public interface MovieService{
//public final String API_KEY = "<apikey>";
public final String SERVICE_END = "https://api.mymovies.org/3/";
#GET("movie/{movieId}??api_key=xyz")
Observable<Response<Movies>> getMovies(#Field("movieId") int movieId);
}
Inside MainActivity
MovieService tmdbService = ServiceFactory.createRetrofitService(MovieService.class, MovieService.SERVICE_END);
Observable<Response<Movies>> responseObservable = tmdbService.getMovies(400);
responseObservable .subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Response<Movies>>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
}
#Override
public void onNext(Response<Movies> moviesResponse) {
}
});
Be sure to add implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0' or whatever version you are using to your dependencies, and then configure retrofit with that converter:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(endpoint)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
Updated
RxJavaCallAdapterFactory was renamed to RxJava2CallAdapterFactory. Changed the snipped above.
For RxJava2 Use compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
For more information on usage https://github.com/JakeWharton/retrofit2-rxjava2-adapter
you should have to use all Rx dependency of latest version , here i am using version 2 (like rxjava2)
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'io.reactivex.rxjava2:rxjava:2.1.9'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
And add one more thing :
addCallAdapterFactory(RxJava2CallAdapterFactory.create())
in Retrofit Api client
like :
retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.build();
From the said Github project page:
Blockquote
This is now DEPRECATED!
Retrofit 2.2 and newer have a first-party call adapter for RxJava 2: https://github.com/square/retrofit/tree/master/retrofit-adapters/rxjava2
now you just need to include in your app/build.gradle file:
compile 'com.squareup.retrofit2:adapter-rxjava2:latest.version'
In my case, it was enough to replace
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
with
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())

apply url to retrofit

I'm writting a retrofit demo.
I have to use "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code" to get code.
when writing rest, I do it like this:
public interface WXService {
#GET("/access_token?grant_type=authorization_code")
Observable<AccessTokenModel> getAccessToken(#Query("appid") String appId,
#Query("secret") String secretId,
#Query("code") String code);
}
public class WXRest {
private static final String WXBaseUrl = "https://api.weixin.qq.com/sns/oauth2/";
private WXService mWXService;
public WXRest() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(WXBaseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
mWXService = retrofit.create(WXService.class);
}
public void getAccessToken(String code) {
mWXService.getAccessToken(Constants.APP_ID, Constants.SECRET_ID, code)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<AccessTokenModel>() {
#Override
public void call(AccessTokenModel accessTokenModel) {
Log.e("WX", "accessToken:" + accessTokenModel.accessToken);
}
});
}
}
but I got an error:
java.lang.IllegalArgumentException: Unable to create call adapter for
rx.Observable
I think it's the way i transform the url wrong.But I don't know how to fix it.
i think you should include adapter-rxjava lib to your gradle dependencies.
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta1'
and then add call adapter factory to your retrofit builder
public WXRest() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(WXBaseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
mWXService = retrofit.create(WXService.class);
}

AbstractMethodError when using RxJavaCallAdapterFactory on Retrofit 2

I get this error:
FATAL EXCEPTION: main
E/AndroidRuntime: java.lang.AbstractMethodError: abstract method not implemented
at retrofit.RxJavaCallAdapterFactory.get(RxJavaCallAdapterFactory.java)
at retrofit.Retrofit.nextCallAdapter(Retrofit.java:189)
at retrofit.Retrofit.callAdapter(Retrofit.java:175)
at retrofit.MethodHandler.createCallAdapter(MethodHandler.java:45)
at retrofit.MethodHandler.create(MethodHandler.java:26)
at retrofit.Retrofit.loadMethodHandler(Retrofit.java:151)
at retrofit.Retrofit$1.invoke(Retrofit.java:132)
at $Proxy0.getPosts(Native Method)
when trying to use RxJavaCallAdapterFactory on retrofit. I'm using com.squareup.retrofit:retrofit:2.0.0-beta1 and com.squareup.retrofit:adapter-rxjava:2.0.0-beta1.
Here's how I created the api interface:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(FORUM_SERVER_URL)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
mForumApi = retrofit.create(ForumApi.class);
The FORUM_SERVER_URL is
private static final String FORUM_SERVER_URL = "http://jsonplaceholder.typicode.com";
my interface method is:
#GET("/posts")
public Observable<List<Post>> getPosts();
I call it via:
mForum.getApi()
.getPosts()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<Post>>() {
#Override
public void onCompleted() {}
#Override
public void onError(Throwable e) {}
#Override
public void onNext(List<Post> posts) {
mView.displayPosts(posts);
}
});
}
getApi returns mForumApi
getPosts is where the error happens, it's the API call
For me it turned out that I was using different beta versions of the components
Changing (notice beta1):
compile 'com.squareup.retrofit:converter-simplexml:2.0.0-beta2'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta1'
to (now beta2)
compile 'com.squareup.retrofit:converter-simplexml:2.0.0-beta2'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
made it work for me.
Stupid error but yeah...

Categories

Resources