导语
昨晚和IOS老大、产品经理以及后台扛把子一起看了《奇异博士》,但是和我这篇文章没半毛钱关系。只是单纯感谢后台潘工请我们去看电影。
本篇记录CountDownLatch同步辅助类的使用
需求
1.Retrofit请求聚合数据API--历史的今天
2.Retrofit请求聚合数据API--微信精选
3.循环请求上述接口3次,用CountDownLatch类改成同步
效果
接口编写
WeChatQueryService
import org.scau.ancientone.entity.Toh;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
/**
* Created by ZP on 2016/11/5.
*/
public interface TohService {
/**
* 历史上的今天
*
* @param key 申请的key
* @param version 版本,目前为1.0
* @param month 查询的月份
* @param day 查询的日
* @return 实体toh
*/
@GET("japi/toh")
Call getToh(@Query("key") String key,
@Query("v") String version,
@Query("month") String month,
@Query("day") String day);
}
WeChatQueryService
import org.scau.ancientone.entity.WeChatQuery;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
/**
* Created by ZP on 2016/11/5.
*/
public interface WeChatQueryService {
/**
* 微信精选
* @param key 申请的key
* @param pno 当前页数
* @param ps 每页返回条数
* @param dtype 数据格式
* @return
*/
@GET("weixin/query")
Call getWeChatQuery(@Query("key") String key,
@Query("pno") String pno,
@Query("ps") String ps,
@Query("dtype") String dtype);
}
接口调用
ApiManager
import org.scau.ancientone.api.TohService;
import org.scau.ancientone.api.WeChatQueryService;
import org.scau.ancientone.constants.ApiAddressPool;
import org.scau.ancientone.entity.Toh;
import org.scau.ancientone.entity.WeChatQuery;
import org.scau.ancientone.http.ServiceFactory;
import retrofit2.Call;
import retrofit2.Callback;
/**
* Created by ZP on 2016/11/5.
*/
public class ApiManager {
private ApiManager() {
mTohService = ServiceFactory.createServiceFrom(TohService.class, ApiAddressPool.API_JUHE_TOH);
mWeChatQuery = ServiceFactory.createServiceFrom(WeChatQueryService.class, ApiAddressPool.API_JUHE_WECHAT);
}
private static ApiManager instance;
public synchronized static ApiManager getInstance() {
if (instance == null) {
return new ApiManager();
}
return instance;
}
private static TohService mTohService;
private static WeChatQueryService mWeChatQuery;
public void getToh(String key, String version, String month, String day, Callback callBack) {
Call call = mTohService.getToh(key,version,month, day);
call.enqueue(callBack);
}
public void getWeChatQuery(String key, Callback callback) {
Call call = mWeChatQuery.getWeChatQuery(key, "", "", "");
call.enqueue(callback);
}
}
ServiceFactory
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by ZP on 2016/11/5.
*/
public class ServiceFactory {
public static T createServiceFrom(Class serviceClass, String endpoint) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(endpoint)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit.create(serviceClass);
}
}
主界面循环3次接口请求
/**
* 循环同步请求五次
*/
private void requestLoop() {
new Thread(new Runnable() {
@Override
public void run() {
try {
for (int i = 1; i <= 3; i++) {
mCountDownLatch = new CountDownLatch(1);
requestToh();
showResult("第" + i + "次请求:历史的今天");
mCountDownLatch.await();
mCountDownLatch = new CountDownLatch(1);
requestWeChatQuery();
showResult("第" + i + "次请求:微信精选");
mCountDownLatch.await();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
/**
* 请求聚合数据 -- 历史的今天
*/
public void requestToh() {
ApiManager.getInstance().getToh(KeyPool.TOH_KEY, "1.0", "11", "5", new Callback() {
@Override
public void onResponse(Call call, Response response) {
System.out.println(response.body().getResult().toString());
showResult("请求成功返回--历史的今天");
mCountDownLatch.countDown();
}
@Override
public void onFailure(Call call, Throwable t) {
System.out.println(t.getMessage());
showResult("请求失败返回--历史的今天");
mCountDownLatch.countDown();
}
});
}
/**
* 请求聚合数据 -- 微信精选
*/
public void requestWeChatQuery() {
ApiManager.getInstance().getWeChatQuery(KeyPool.WECHAT_KEY, new Callback() {
@Override
public void onResponse(Call call, Response response) {
System.out.println(response.body().getResult().toString());
showResult("请求成功返回--微信精选");
mCountDownLatch.countDown();
}
@Override
public void onFailure(Call call, Throwable t) {
System.out.println(t.getMessage());
showResult("请求失败返回--微信精选");
mCountDownLatch.countDown();
}
});
}
/**
* 更新界面
*
* @param s
*/
private void showResult(final String s) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mTextView.setText(mTextView.getText().toString() + "\n" + s);
}
});
}
当创建 CountDownLatch 对象时,对象使用构造函数的参数来初始化内部计数器。每次调用 countDown() 方法, CountDownLatch 对象内部计数器减一。当内部计数器达到0时, CountDownLatch 对象唤醒全部使用 await() 方法睡眠的线程们。