Posts

Showing posts with the label Retrofit

Android Retrofit 2 + RxJava: Listen To Endless Stream

Answer : Here my solution: You can use the @Streaming annotation: public interface ITwitterAPI { @GET("/2/rsvps") @Streaming Observable<ResponseBody> twitterStream(); } ITwitterAPI api = new Retrofit.Builder() .baseUrl("http://stream.meetup.com") .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build().create(ITwitterAPI.class); With @Streaming we can get raw input From ResponseBody . Here my function to wrap body divided by lines with events: public static Observable<String> events(BufferedSource source) { return Observable.create(new Observable.OnSubscribe<String>() { @Override public void call(Subscriber<? super String> subscriber) { try { while (!source.exhausted()) { subscriber.onNext(source.readUtf8Line()); } subscriber.onCompleted(); } catch (IOException e)...

Call Another Rest Api From My Server In Spring-Boot

Answer : This website has some nice examples for using spring's RestTemplate. Here is a code example of how it can work to get a simple object: private static void getEmployees() { final String uri = "http://localhost:8080/springrestexample/employees.xml"; RestTemplate restTemplate = new RestTemplate(); String result = restTemplate.getForObject(uri, String.class); System.out.println(result); } Instead of String you are trying to get custom POJO object details as output by calling another API/URI , try the this solution. I hope it will be clear and helpful for how to use RestTemplate also, In Spring Boot , first we need to create Bean for RestTemplate under the @Configuration annotated class. You can even write a separate class and annotate with @Configuration like below. @Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate(RestTemplateBuilder builder) { return builder.build(); } } The...