Retrofit的简单使用

Retrofit的简单使用
1.引入
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'		
    compile 'com.squareup.retrofit2:converter-scalars:2.1.0'
    //ConverterFactory的String依赖包
    compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'		//支持rx
    compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'		//拦截器
    compile 'com.github.franmontiel:PersistentCookieJar:v1.0.0'	//设置cookie

2.配置
	
public class CampRetrofit {
    final CampService request;
    final ClearableCookieJar cookieJar;

    CampRetrofit() {
        cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(AppConfig.getContext()));
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
                .cookieJar(cookieJar);  //设置cookie

        if (CampFactory.isDebug) {
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                @Override
                public void log(String message) {	//设置拦截 以log形式打印
                    LogUtils.info("HttpLoggingInterceptor --> " + message);
                }
            });
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
            httpClient.addInterceptor(logging);
            httpClient.connectTimeout(10, TimeUnit.SECONDS);
        }

        OkHttpClient client = httpClient.build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(CampFactory.BASE_API_URL)	
                .addConverterFactory(GsonConverterFactory.create())	//支持gson
                .addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()))	//支持rx
                .client(client)
                .build();
        request = retrofit.create(CampService.class);

    }
public CampService getCampService() {
        return request;
    }

    public ClearableCookieJar getCookieJar() {
        return cookieJar;
    }
}

public class CampFactory {
    private static CampService request;
    private static ClearableCookieJar cookieJar;

    public final static boolean isDebug = true;
    public final static String BASE_URL = "";
    public final static String BASE_API_URL =  BASE_URL + "/user_api/";

    public static CampService getRequestSingleton() {
        if(request == null) {
            synchronized (CampFactory.class) {
                if (request == null) {
                    request = new CampRetrofit().getCampService();
                }
            }
        }
        return request;
    }

    public static ClearableCookieJar getCookieJarSingleton() {
        if(cookieJar == null) {
            synchronized (CampFactory.class) {
                if (cookieJar == null) {
                    cookieJar = new CampRetrofit().getCookieJar();
                }
            }
        }
        return cookieJar;
    }

}

3.url接口

public interface CampService {
    @FormUrlEncoded
    @POST("sms")
        //获取手机短信(4位数)
    Observable<JsonObject> getSms(@Field("account") String account);

    @FormUrlEncoded
    @POST("register")
        //注册会员
    Observable<JsonObject> register(@Field("account") String account,
                                    @Field("name") String name,
                                    @Field("sms") String sms,
                                    @Field("recmd_account") String recmd_account
    );

   @GET("refresh")
    Observable<JsonObject> refresh(@Query("userid") String userid,
                                   @Query("sign") String sign,
                                   @Query("timestamp") String timestamp
    );

   @GET("company/{id}")
//单个企业列表详情
    Observable<JsonObject> getCompanyDetails(@Path("id") String id); //企业ID

}




4.使用
CampService request = CampFactory.getRequestSingleton();

request.getBroker(member.data.id, member.sign, member.timestamp)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Action1<JsonObject>() {
            @Override
            public void call(JsonObject jsonObject) {
                
            }
        });






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值