Retrofit2.0 converterfactory

本文介绍如何在Android应用中使用Retrofit结合RxJava进行网络请求,包括配置依赖、创建Retrofit实例、自定义类型转换器等步骤,并提供了一个具体的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先贴出dependencies :

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.1.0'
    //google-gson
    compile 'com.google.code.gson:gson:2.7'
    //retrofit
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    //retrofit-string
    compile 'com.squareup.retrofit2:converter-scalars:2.1.0'
    //retrofit-converter-gson
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    //retrofit-rxJava-converter
    compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
    //rxjava
    compile 'io.reactivex:rxandroid:1.1.0'
    compile 'io.reactivex:rxjava:1.1.0'
}

新建一个Retrofit对象;

Retrofit retrofit = new Retrofit.Builder() .baseUrl(API).build();

添加类型转换的工厂类:
支持string类型
.addConverterFactory(ScalarsConverterFactory.create())

支持gson格式

.addConverterFactory(GsonConverterFactory.create(customGsonInstance))

支持observable类型

.addCallAdapterFactory(RxJavaCallAdapterFactory.create())

整体代码为:

Retrofit retrofit = new Retrofit
.Builder()
.baseUrl(API)                    
.addConverterFactory(ScalarsConverterFactory.create())                   .addConverterFactory(GsonConverterFactory.create(customGsonInstance))                        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                        .build();

其中的 customGsonInstance是返回的string要转换成gson对象;
上代码

final Gson customGsonInstance = new GsonBuilder()
                .registerTypeAdapter(GitModel.class,
                        new MarvelResultsDeserializer<GitModel>())
                .create();
class MarvelResultsDeserializer<T> implements JsonDeserializer<GitModel> {
        @Override
        public GitModel deserialize(JsonElement je, Type typeOfT,
                                   JsonDeserializationContext context) throws JsonParseException {
            // 转换Json的数据, 获取内部有用的信息
            //JsonElement results = je.getAsJsonObject();
            return new Gson().fromJson(je, typeOfT);
        }
    }

作用是把gson 格式的文件最终转化为GitModel的对象;

这里要提一下,这个GitModel 是通过GsonFormat插件来生成的,很方便,需要的请自行百度;

最终转化的结果要和自定义的interface相符合;

public interface GitApi {

    @GET("/users/{user}")      // here is the other url part.best way is to start using /
    Observable<GitModel> getFeed(@Path("user") String user);
    // string user is for passing values from edittext for eg: user=basil2style,google
    // response is the response from the server which is now in the POJO
}

其他的convert 依赖

•Gson: com.squareup.retrofit2:converter-gson
•Jackson: com.squareup.retrofit2:converter-jackson
•Moshi: com.squareup.retrofit2:converter-moshi
•Protobuf: com.squareup.retrofit2:converter-protobuf
•Wire: com.squareup.retrofit2:converter-wire
•Simple XML: com.squareup.retrofit2:converter-simplexml
•Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars

Github代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值