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) {
}
});