研究了一段retrofit,但是还是有一点不清楚,就是接口不确定怎么写,
假如有个接口请求连接如下 :
http://test.com/indiana-front/user/login10?data={"phone":"16111110000","smsType":"USERLOGIN","validCode":"1"}&token=9FF93C4DE6B54A8F9CCE750E324FBF1C&mac=868227023024375&cfcnl=Official&version=1.1.0&requestType=1
其中:token,mac,cfcnl,version,requestType都是放在头的
请问该怎么写接口
http://blog.csdn.net/zuck_t/article/details/50718774
我直接把我的拷贝给你吧 代码 处理头部在最下面处理的
public class RetrofitManager {
//时间超时
public static final int TIMEOUT = 30;
private APIService mApiService;
private static volatile OkHttpClient mOkHttpClient;
//保证多个地址的
public static Map managers = new HashMap<>();
public static RetrofitManager getInstance(String url) {
RetrofitManager instance = managers.get(url);
if (instance == null) {
instance = new RetrofitManager(url);
managers.put(url, instance);
}
return instance;
}
public static RetrofitManager getInstance() {
return getInstance(UstarURL.SERVICE_URL);
}
private RetrofitManager(String url) {
initOkHttpClient();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(mOkHttpClient)
.build();
mApiService = retrofit.create(APIService.class);
}
public static APIService getAPIService() {
return getInstance().mApiService;
}
public static APIService getAPIService(String url) {
return getInstance(url).mApiService;
}
private void initOkHttpClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
if (mOkHttpClient == null) {
synchronized (RetrofitManager.class) {
if (mOkHttpClient == null) {
// 指定缓存路径,缓存大小100Mb
Cache cache = new Cache(new File(UStarApplication.getInstance().getApplicationContext().getCacheDir(), "HttpCache"), 1024 * 1024 * 100);
mOkHttpClient = new OkHttpClient.Builder()
.cache(cache)
//统一处理请求头
.addInterceptor(netInterceptor)
.retryOnConnectionFailure(true)
.connectTimeout(TIMEOUT, TimeUnit.SECONDS)
.build();
}
}
}
}
//头部统一处理
Interceptor netInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
//网络不可用
if (!UStarUtils.isNetworkAvailable(UStarApplication.getInstance().getApplicationContext())) {
request=request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
}
if(SPUtils.isLogin(UStarApplication.getInstance().getApplicationContext())) {
request= request.newBuilder().addHeader("rsa","").build();
}else {
request=request.newBuilder().addHeader("rsa",SPUtils.getUser(UStarApplication.getInstance().getApplicationContext()).token+"").build();
}
return chain.proceed(request);
}
};
}