![]() Now that we have our Retrofit object, we can construct our service call, let's take a look at how to do this the synchronous way: UserService service = retrofit.create(UserService.class) Ĭall callSync = service.getUser("eugenp") We could also add headers and interceptors for every call, which we're going to see in our authentication section. This is going to take care of connecting to the server and the sending and retrieval of information. The last dependency is OKHttpClient – which is an HTTP & HTTP/2 client for Android and Java applications. For a list of already implemented factories, we can have a look here. It's important to note that different factories serve different purposes, so keep in mind that we can also use factories for XML, proto-buffers or even create one for a custom protocol. In this example, we're going to use the GsonConverterFactory, which is going to map our JSON data to the User class we defined earlier. ![]() It needs the base URL which is going to be used for every service call and a converter factory – which takes care of the parsing of data we're sending and also the responses we get. Retrofit provides a convenient builder for constructing our required object. addConverterFactory(GsonConverterFactory.create()) Retrofit retrofit = new Retrofit.Builder() To construct an HTTP request call, we need to build our Retrofit object first: OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |