Allen Vork
Allen Vork

Reputation: 1546

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.

Upvotes: 0

Views: 181

Answers (1)

surya
surya

Reputation: 116

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);
    }

Upvotes: 2

Related Questions