dimanche 10 juillet 2016

Exception handling in rxjava

I am trying to get accustomed to rxjava and I am trying to call the below QuoteReader in an Observable. I am not sure how to handle the exception thrown,

public class QuoteReader {
   public Map<String, Object> getQuote() throws IOException{
       OkHttpClient okHttpClient = new OkHttpClient();
       Request request = new Request.Builder().url("http://quotes.rest/qod.json").build();
       Gson gson = new Gson();
       Map<String, Object> responseMap = null;

       try(Response response = okHttpClient.newCall(request).execute()) {
         responseMap = gson.fromJson(response.body().string(), Map.class);
         System.out.println("response map : "+responseMap);
       } catch(IOException ioe) {
         ioe.printStackTrace();
         throw ioe;
       } finally {
         okHttpClient = null;
         request = null;
       }
    return responseMap;
  }
}

The following is the rx code I am trying to write,

rx.Observable.just(new QuoteReader().getQuote()) //compile time error saying unhandled exception
              .subscribe(System.out::println);

How should I update the code to handle the exception. Thanks!

Aucun commentaire:

Enregistrer un commentaire