[Android Studio] How to Call REST API with Retrofit
Retrofit is a REST Client library (Helper Library) used in Android and Java to create an HTTP request and also to process the HTTP response from a REST API.
We can also use retrofit to receive data structures other than JSON, for example SimpleXML and Jackson.
Here are the steps to call REST API using Retrofit in Android.
5. We have completed the interface. Now we want to use it to actually make a request.
Create a Retrofit object and an APIService object as global variables.
We can also use retrofit to receive data structures other than JSON, for example SimpleXML and Jackson.
Advantages of using Retrofit
- It manages network communication systematically and efficiently.
- You can request data in certain format, and parse the response data.
- It is relatively easier to handle errors.
Here are the steps to call REST API using Retrofit in Android.
1. Add INTERNET permission in AndroidManifest.xml
2. Open Gbuild.gradle (:app) and add the libraries inside the dependencies.
3. Create an APIResponse class to parse the result data later.
In my case, the response format is like
{"status":200,"result":"success",detail":"this is the response"}.
4. Create an interface to manage URL and API request part.
- First one requests GET to "http://api.somehost.com/get_new_target" without any parameters.
- Second one requests POST to "http://api.somehost.com/upload_record" with String parameter "name" and filedata.
Instead of default ResponseBody, we will use APIResponse class that we've created at 3.
<uses-permission android:name="android.permission.INTERNET" />
2. Open Gbuild.gradle (:app) and add the libraries inside the dependencies.
implementation 'com.squareup.retrofit2:retrofit:2.1.0' implementation 'com.squareup.retrofit2:converter-gson:2.0.0'
3. Create an APIResponse class to parse the result data later.
In my case, the response format is like
{"status":200,"result":"success",detail":"this is the response"}.
import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; public class APIResponse { @SerializedName("status") @Expose private int rstatus; @SerializedName("result") @Expose private String result; @SerializedName("detail") @Expose private String detail; public String getDetail() { return detail; } public String getResult() { return result } public int getStatus() { return rstatus; } }
4. Create an interface to manage URL and API request part.
- First one requests GET to "http://api.somehost.com/get_new_target" without any parameters.
- Second one requests POST to "http://api.somehost.com/upload_record" with String parameter "name" and filedata.
Instead of default ResponseBody, we will use APIResponse class that we've created at 3.
import okhttp3.MultipartBody; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Multipart; import retrofit2.http.POST; import retrofit2.http.Part; public interface APIService { public static final String API_URL = "http://api.somehost.com"; @GET("/get_new_target") Call<APIResponse>getTarget(); @POST("upload_record") @Multipart Call<APIResponse>updateResult( @Part("name") String name, @Part MultipartBody.Part audiofile ); }
5. We have completed the interface. Now we want to use it to actually make a request.
Create a Retrofit object and an APIService object as global variables.
public class MainActivity extends AppCompatActivity { APIService apiService; Retrofit retrofit;
Call<APIResponse> target = apiService.getTarget(); target.enqueue(new Callback<APIResponse>(){ @Override public void onResponse(Call<APIResponse> cal, Response<APIResponse> response) { try{
//do something with response! } catch (Exception e){ e.printStackTrace(); } } @Override public void onFailure(Call<APIResponse> call, Throwable t) { LogWrapper.e("APIError",t.toString()); } });
Comments
Post a Comment