java - Exception handling with Retrofit2 -


i learning how use retrofit2 in trouble.

is way catch response object on retrofit2.retrofit , throw exception if http response code out of range 2xx?

i implementing jax-rs rest services rest methods call rest apis collect information , handle http error on jax-rs side:

public class hellorest {      @get("/ping")     public string ping() throws ioexception {         helloservice client = helloservicebuilder.getinstance();         string response = client.sayhello().execute().body();         logger.info(response);     }      @get ("echo/{msg}")     public string echo(@pathparam("msg") string msg) throws ioexception {         helloservice client = helloservicebuilder.getinstance();         string response = client.echo(msg).execute().body();         logger.info(response);         return response;     } } 

first, have realized execute() method throws ioexception had add rest methods signatures. okay, can handle jax-rs.

but best way handle errors related http responses response code out of range 2xx?

i not want write repeated code block check http response code time when use retrofit2 this:

response<string> response = client.ping().execute(); int responsecode = response.code(); if (responsecode < 200 && responsecode > 299) {     throws anyexception("..."); }  string serverresponse = response.body(); ... 

can add retrofit.builder() code block handle situation on general way somehow?

public final class helloservicebuilder {      private static final string service_url = "...";      private helloservicebuilder() {         // nothing     }      public static helloservice getinstance() {         retrofit retrofit = new retrofit.builder()             .baseurl(service_url)             .addconverterfactory(scalarsconverterfactory.create())             .how-to-check-responses-here?             .build();          return retrofit.create(helloservice.class);     } } 


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -