![]() ![]() ![]() The most important method here is execute, which is used to execute a call synchronously and will block the current thread while transferring the data. ![]() Here, we can see how Retrofit takes care of the construction of our service interface by injecting the code necessary to make the request, based on our previous annotations.Īfter that, we get a Call object which is the one used for executing the request to the GitHub API. 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. To make a Retrofit request, youâll need the following: A Retrofit class: This is where youâll create a Retrofit instance and define the base URL that your app will use for all of its HTTP. 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. To make retrofit use a Callback converting the possible success/errors cases to the Result type we need to wrap the callback in a callAdapter.Retrofit provides a convenient builder for constructing our required object. addConverterFactory(GsonConverterFactory.create()) Create the Retrofit Instance To issue network requests to a REST API with Retrofit, we need to create an instance using the Retrofit.Builder class and configure it with a base URL. 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. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |