Retrofit 2.0初识

Retrofit 2.0 有意思返回都是Call  因为底层依赖okhhtp

最新版 

compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
 
解析:retrofit不再依赖Gson 所以你需要导入gson解析工厂
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'

Rerofit 2.0支持RxAndroid 相比支持RxJava更加适合android平台,其差异可以百度RxAndroid对比RxJava
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
compile 'io.reactivex:rxandroid:1.0.1'

Rerofit 2.0 搭建
package com.retrofit.test.retrofit;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.retrofit.test.constant.Constants;
import com.retrofit.test.model.User;

import java.util.Map;

import retrofit.Call;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import rx.Observable;

/**
 * ClassName RetrofitService
 * Description
 * Company
 * author  youxuan  E-mail:xuanyouwu@163.com
 * date createTime:2015/12/24 10:42
 * version
 */
public class RetrofitService {
    private static RetrofitService retrofitService;
    private ApiService apiService;

    public static RetrofitService getInstance() {
        if (retrofitService == null) {
            retrofitService = new RetrofitService();
        }
        return retrofitService;
    }

    public RetrofitService() {
        initRetrofit();
    }

    private void initRetrofit() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                //.addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(MyGsonConverter.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        apiService = retrofit.create(ApiService.class);
    }
}

声明API:

<pre name="code" class="java">package com.retrofit.test.retrofit;


import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.retrofit.test.model.User;

import java.util.Map;

import retrofit.Call;
import retrofit.Callback;
import retrofit.http.Body;
import retrofit.http.Field;
import retrofit.http.FieldMap;
import retrofit.http.FormUrlEncoded;
import retrofit.http.GET;
import retrofit.http.Header;
import retrofit.http.Headers;
import retrofit.http.Multipart;
import retrofit.http.POST;
import retrofit.http.Part;
import retrofit.http.PartMap;
import retrofit.http.Path;
import retrofit.http.Query;
import retrofit.http.QueryMap;
import rx.Observable;

/**
 * ClassName GitHubService
 * Description
 * Company
 * author  youxuan  E-mail:xuanyouwu@163.com
 * date createTime:2015/12/24 10:40
 * version
 */
public interface ApiService {

    //直接get访问一个地址  返回json
    @GET("/index.php")
    Call<JsonObject> getSynchronous();

    //返回User
    @GET("/index.php")
    Call<User> getSynchronous2();

    //返回User Observable
    @GET("/index.php")
    Observable<User> getgetObservable();

    // get 方式拼接地址
    @GET("/{next}index.php")
    Call<JsonObject> getPathMatch(@Path("next") String next);

    // get 方式传参
    @GET("/{next}index.php")
    Call<JsonObject> getSendParams(@Path("next") String next, @Query("sort") String sort);

    //在get地址后面添加参数,使用QueryMap来添加多个参数,当然也可以使用多个Query来替代
    @GET("/{next}index.php")
    Call<JsonObject> getSendParams2(@Path("next") String next, @QueryMap Map<String, String> params);

    /**************************
     * 下面的方法为测试post方法,传递值以及获取callback的方式
     ************************************/

    //直接访问一个post地址,使用Body传递参数,
    //需要注意的是,使用这种方式来传递参数的话,需要自定义Converter,具体请参考MyGsonConverter
    @POST("/postParams.php")
    Call<JsonElement> callPostParamsBody(@Body User user);


    //直接访问一个post地址,使用Field传递参数
    @POST("/postParams.php")
    @FormUrlEncoded
    Call<JsonElement> callPostParams(@Field("name") String name, @Field("age") int age);

    //直接访问一个post地址,使用FieldMap传递参数  可以传递多个参数
    @POST("/postParams.php")
    @FormUrlEncoded
    Call<JsonElement> callPostParams(@FieldMap Map<String, String> map);


    //注意表单
    @POST("/postParams.php")
    @Multipart
//注意阅读Part的注释,int等其他类型,需要特殊处理
    Call<JsonElement> callPostMultiPart(@Part("name") String name, @Part("age") String age);


    @POST("/postParams.php")
    @Multipart
    Call<JsonElement> callPostMultiPart(@PartMap Map<String, String> params);


    /**************************** 下面的方法是测试添加header的方法 ************************************************************/
    @Headers("Cache-Control: max-age=640000")
    @GET("/index.php")
    Call<String> callIndexAddHeaders(Callback<String> call);

    @Headers({
            "Accept: application/vnd.github.v3.full+json",
            "User-Agent: Retrofit-Sample-App"
    })
    @GET("/index.php")
    Call<String> callIndexAddHeaders2(Callback<String> call);


    @GET("/index.php")
    Call<String> callIndexAddHeaders3(@Header("Cache-Control") String headers, Callback<String> call);

}


使用方法提供:
<pre name="code" class="java">package com.retrofit.test.retrofit;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.retrofit.test.constant.Constants;
import com.retrofit.test.model.User;

import java.util.Map;

import retrofit.Call;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import rx.Observable;

/**
 * ClassName RetrofitService
 * Description
 * Company
 * author  youxuan  E-mail:xuanyouwu@163.com
 * date createTime:2015/12/24 10:42
 * version
 */
public class RetrofitService {
    private static RetrofitService retrofitService;
    private ApiService apiService;

    public static RetrofitService getInstance() {
        if (retrofitService == null) {
            retrofitService = new RetrofitService();
        }
        return retrofitService;
    }

    public RetrofitService() {
        initRetrofit();
    }

    private void initRetrofit() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                //.addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(MyGsonConverter.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        apiService = retrofit.create(ApiService.class);
    }


    public Call<JsonObject> getSynchronous() {
        return apiService.getSynchronous();
    }

    public Call<User> getSynchronous2() {
        return apiService.getSynchronous2();
    }


    public Observable<User> getObservable() {
        return apiService.getgetObservable();
    }


    public Call<JsonObject> getMatchPath(String path) {
        return apiService.getPathMatch(path);
    }

    public Call<JsonObject> getSendParam(String path, String sort) {
        return apiService.getSendParams(path, sort);
    }

    public Call<JsonObject> getSendParam2(String path, Map<String, String> map) {
        return apiService.getSendParams2(path, map);
    }

    public Call<JsonElement> callPostParamsBody(User user) {
        return apiService.callPostParamsBody(user);
    }

    public Call<JsonElement> callPostParams(String name, int age) {
        return apiService.callPostParams(name, age);
    }

    public Call<JsonElement> callPostParams(Map<String, String> map) {
        return apiService.callPostParams(map);
    }

    public Call<JsonElement> callPostMultiPart(String name, String age) {
        return apiService.callPostMultiPart(name, age);
    }

    public Call<JsonElement> callPostMultiPart(Map<String, String> map) {
        return apiService.callPostMultiPart(map);
    }
}



 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亚洲小炫风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值