Coder Social home page Coder Social logo

retrofit-converter's Introduction

Download

retrofit-converter

module composite-converter 说明

Gradle依赖:

dependencies {
    implementation 'com.github.woodyhi.retrofit:composite-converter:0.1.3'
}

示例

创建retrofit,并且使用了RxJava,CompositeConverterFactory.create(Factory f)设置默认转换工厂

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(CompositeConverterFactory.create(GsonConverterFactory.create()))
                .client(okHttpClient)
                .build();

假设接口服务


public interface ApiService {
    // 使用默认转换工厂
    @GET("user/{username}")
    Observable<User> getUser(@Path("username") String username);

    // 动态指定结果转换工厂
    @ResponseConverter(ScalarsConverterFactory.class)
    @GET("text/hello")
    Observable<String> hello();

    // 响应结果动态设置转换工厂
    @ResponseConverter(ScalarsConverterFactory.class)
    @POST("send")
    Observable<String> sendUserJson(@Body User user);

    // 请求体、响应结果 都动态指定转换工厂
    @RequestConverter(SimpleXmlConverterFactory.class)
    @ResponseConverter(ScalarsConverterFactory.class)
    @POST("xml")
    Observable<String> sendUserXml(@Body User user);
}

@RequestConverter 指定请求体的转换工厂,@ResponseConverter 指定结果的转换工厂,可以不加注释使用默认转换工厂

注:注解中设置的XxxFactory.class,CompositeConverterFactory会调用其XxxFactory.create()方法。

retrofit-converter's People

Contributors

woodyhi avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.