Updated 24 November 2017
If you are looking for one of the latest libraries for making HTTP connection in Android or Java, then you are at the right place. Retrofit is new library provide by Square Inc which is much better than its competitors in the market and also easy to use.
To use this library you need to add the dependency in your module level build Gradle
1 2 3 4 |
compile 'com.squareup.retrofit2:retrofit:2.0.2' compile 'com.squareup.retrofit2:converter-gson:2.0.2' compile 'com.squareup.okhttp3:logging-interceptor:3.3.1' compile 'com.squareup.okhttp3:okhttp-urlconnection:3.0.0-RC1' |
Here We have added four dependencies. The first one is to create HTTP connection using retrofit, the second one is to use Gson to convert response data directly into model which are used during data binding, the third one is to log the requests and the fourth one is actually used to create HTTP connection but we have used it to manage the cookies in our requests.
To make HTTP requests, it’s better to make a retrofit client which can be used again and again for all the requests instead of writing the same code again everywhere to make each HTTP request.
Here is an example of retrofit client which we have created
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public class RetrofitClient { private static Retrofit retrofit = null; public static Retrofit getClient() { if (retrofit == null) { CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient.Builder oktHttpClient = new OkHttpClient.Builder() .connectTimeout(connection_time_in_min, TimeUnit.MINUTES) .writeTimeout(write_time_in_min, TimeUnit.MINUTES) .readTimeout(read_time_in_min, TimeUnit.MINUTES) .cookieJar(new JavaNetCookieJar(cookieManager)); oktHttpClient.addInterceptor(logging); retrofit = new Retrofit.Builder() .baseUrl(your_base_url) .addConverterFactory(GsonConverterFactory.create()) .client(oktHttpClient.build()) .build(); } return retrofit; } } |
The above retrofit client can be used in the whole android project to make requests.
Next, you have to create the request
Your request method can be of any type like GET, POST, PUT, DELETE, and HEAD. Retrofit provides annotation for every request method.
1 2 3 4 5 6 7 8 9 10 |
@FormUrlEncoded @POST(your_url_without_base_url) Call<your_response_data_model> your_request_name( @Header("your_key_name") String your_variable_name , @Header("your_key_name") String your_variable_name , @Header("your_key_name") String your_variable_name , @Field("your_key_name") int your_variable_name , @Field("your_key_name") int your_variable_name ); |
If you notice in the above example, the request contains some header and fields ( i.e. Form Data ). The annotation at the top of the request ( @FormUrlEncode ) provides you the facility to encode form data in the request. The annotation @POST defines the method type followed by the request URL which does not contain base URL. The model your_reponse_data model which is mostly used when you have implemented data binding. To create a model you can take the help of jsonschema2pojo, all you need to do is insert you response data and it will automatically create a data model for you with setters and getters.
Now when you request is ready its time to make some requests. So in your Class, you need to take the instance of your retrofit client, call the request method using the retrofit client and pass the parameters in it. And also define a callback method which will handle the response of your request.
Look at the code segment below for better understanding :
1 2 3 4 5 6 7 8 9 |
Call<your_response_data_model> call = RetrofitClient.getClient().create(your_class_where_request_is_created.class).getAccountInfo( your_param_1 , your_param_2 , your_param_3 , your_param_4 , your_param_5 ); call.enqueue(your_callback_method_name); |
And your callback method should look like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
private Callback<your_response_data_model> your_callback_method_name = new Callback<your_response_data_model>() { @Override public void onResponse(Call<your_response_data_model> call, Response<your_response_data_model> response) { //perform task on response //To get the response response.body().getter_from_your_response_data_model(); } @Override public void onFailure(Call<AccountInfoResponseData> call, Throwable t) { t.printStackTrace(); //preform tak on request failure } }; |
response.body() will provide you the object of your_response_data_model and using its getter and setter you can easily get and set the data from and into the model.
That’s all you need to create an HTTP request using Retrofit library.
Thank you very much. This is Vedesh Kumar signing off.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.