Unable to create call adapter for class example.Simple

后端 未结 14 1126
隐瞒了意图╮
隐瞒了意图╮ 2020-12-04 23:14

I am using retrofit 2.0.0-beta1 with SimpleXml. I want the retrieve a Simple (XML) resource from a REST service. Marshalling/Unmarshalling the Simple object with SimpleXML w

相关标签:
14条回答
  • 2020-12-04 23:42

    Add the following dependencies for retrofit 2

     compile 'com.squareup.retrofit2:retrofit:2.1.0'
    

    for GSON

     compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    

    for observables

    compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
    

    In your case for XML , you would have to include the following dependencies

     compile 'com.squareup.retrofit2:converter-simplexml:2.1.0'
    

    Update the service call as below

    final Retrofit rest = new Retrofit.Builder()
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .addConverterFactory(SimpleXmlConverterFactory.create())
        .baseUrl(endpoint)
        .build();
    SimpleService service = rest.create(SimpleService.class);
    
    0 讨论(0)
  • 2020-12-04 23:42

    Just to make the Call examples clearer for people who are migrating, not using Rx, or who want synchronous calls - Call essentially replaces (wraps) Response, meaning:

    Response<MyObject> createRecord(...);
    

    becomes

    Call<MyObject> createRecord(...);
    

    and not

    Call<Response<MyObject>> createRecord(...);
    

    (which will still require an adapter)


    The Call will then allow you to still use isSuccessful as it actually returns a Response. So you can do something like:

    myApi.createRecord(...).execute().isSuccessful()
    

    Or access your Type (MyObject) like:

    MyObject myObj = myApi.createRecord(...).execute().body();
    
    0 讨论(0)
  • 2020-12-04 23:43

    If you are not using RxJava it properly makes no sense to add RxJava just for retrofit. 2.5.0 has support for CompletableFuture built in which you can use without adding any other library or adapter.

    build.gradle.kts

    implementation("com.squareup.retrofit2:retrofit:2.5.0")
    implementation("com.squareup.retrofit2:retrofit-converters:2.5.0")
    

    Api.kt

    interface Api {
        @GET("/resource")
        fun listCompanies(): CompletableFuture<ResourceDto>
    }
    

    Usage:

    Retrofit.Builder()
       .addConverterFactory(SimpleXmlConverterFactory.create())
       .baseUrl("https://api.example.com")
       .build()
       .create(Api::class.java)
    
    0 讨论(0)
  • 2020-12-04 23:44

    You can implement a Callback, get the Simple from onResponse function.

    public class MainActivity extends Activity implements Callback<Simple> {
    
        protected void onCreate(Bundle savedInstanceState) {
            final Retrofit rest = new Retrofit.Builder()
                        .addConverterFactory(SimpleXmlConverterFactory.create())
                        .baseUrl(endpoint)
                        .build();
            SimpleService service = rest.create(SimpleService.class);
            Call<Simple> call = service.getSimple("572642");
            //asynchronous call
            call.enqueue(this);
    
            return true;
        }
    
        @Override
        public void onResponse(Response<Simple> response, Retrofit retrofit) {
           // response.body() has the return object(s)
        }
    
        @Override
        public void onFailure(Throwable t) {
            // do something
        }
    
    }
    
    0 讨论(0)
  • 2020-12-04 23:45

    In case of Kotlin and coroutines this situation happened when I forgot to mark api service function as suspend when I call this function from CoroutineScope(Dispatchers.IO).launch{}:

    Usage:

        val apiService = RetrofitFactory.makeRetrofitService()
    
        CoroutineScope(Dispatchers.IO).launch {
    
            val response = apiService.myGetRequest()
    
            // process response...
    
        }
    

    ApiService.kt

    interface ApiService {
    
           @GET("/my-get-request")
           suspend fun myGetRequest(): Response<String>
    }
    
    0 讨论(0)
  • 2020-12-04 23:45

    In my case I used

    com.squareup.retrofit2:adapter-rxjava:2.5.0 //notice rxjava
    

    instead of

    com.squareup.retrofit2:adapter-rxjava2:2.5.0 //notice rxjava2
    

    you should be using com.squareup.retrofit2:adapter-rxjava2:2.5.0 when using io.reactivex.rxjava2

    0 讨论(0)
提交回复
热议问题