1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| public class NetWorkTest {
@Test public void testIp() { List<String> ips = Arrays.asList( "104.194.84.57" , "14.194.84.55" , "13.194.84.55" ); CountDownLatch latch = new CountDownLatch(ips.size()); CompositeDisposable compositeDisposable = new CompositeDisposable();
compositeDisposable.addAll(ips.stream() .map(s -> getDisposable(latch, s)) .toArray(Disposable[]::new)); try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } }
private Disposable getDisposable(CountDownLatch latch, String ip) { return NetWork.get("http://ip-api.com/", IpService.class) .getIpDetail(ip) .subscribeOn(Schedulers.io()) .observeOn(Schedulers.computation()) .subscribe(ipDetail -> { latch.countDown(); System.out.println("city:" + ipDetail.getCity()); }, throwable -> { latch.countDown(); System.out.println("error:" + throwable); }); } }
|