Networking
Networking service for the application uses Retofit 2 library ,okhttp 3 library and gson libray. Retrofit is a REST Client for Android and Java by Square. It makes it relatively easy to retrieve and upload JSON (or other structured data) via a REST based webservice. In Retrofit you configure which converter is used for the data serialization. Typically for JSON you use GSon, but you can add custom converters to process XML or other protocols. Retrofit uses the OkHttp library for HTTP requests.
To work with Retrofit you need basically three classes.
Model class which is used to map the JSON data
Interfaces which defines the possible HTTP operations
Retrofit.Builder class - Instance which uses the interface and the Builder API which allows defining the URL end point for the HTTP operation.
Model class
This uses java objects class (POJO) that has been allocated getter and setter methods to receive and give data.It is included with gson serialize annotation.
public class Payer {
@SerializedName("token")
@Expose
private String token;
@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Interfaces
Every method of an interface represents one possible API call. It must have a HTTP annotation (GET
,POST
, PUT
.) to specify the request type and the relative URL. The return value wraps the response in a _Call _object with the type of the expected result.
public interface ApiInterface {
@POST("users/login/")
Call<Login> getLogin(@Body LoginRequest loginrequest);
@GET("rss.xml")
Call<RSSFeed> getNews();
@GET("services/get_payers/")
Call<List<Payer>> getPayers(@Header("Authorization") String authorization);
@GET("services/insure_check/{insurance_id}/{member_id}/{type}/")
Call<Check> getCheck(@Path("insurance_id") String insuranceId, @Path("member_id") String memberId, @Path("type") String type,@Header("Authorization") String authorization);
@POST("providers/searchdoctor/")
Call<List<DoctorSearch>> getDoctorSearch(@Body SearchDoctorRequest searchDoctorRequest);
@POST("getdrug/")
Call<List<Drug>> getDrug (@Body DrugRequest drugRequest);
@POST("providers/searchbranch/")
Call<List<Hospital>> getHospital (@Body HospitalRequest hospitalRequest);
@POST("providers/search_doctor_by_name/")
Call<List<DoctorSearch>> getDoctor(@Body DoctorRequest doctorRequestRequest);
@PUT("/users/update_user_key/")
Call<Check> getFingerprintUpdate(@Body FingerprintUpdate fingerprintUpdate);
@POST("/users/biometric_login/")
Call<Login> getBiometricLogin(@Body LoginRequest loginrequest);
}
Retrofit Builder.Class
Retrofit can also be extended by adapters to get involved with other libraries like RxJava 2.x, Java 8 and Guava.In order to add an adapter theretrofit2.Retrofit.Builder.addCallAdapterFactory(Factory)
method has to be used.
Retrofit can be configured to use a specific converter. This converter handles the data (de)serialization. Several converters are already available for various serialization formats.
public class ApiClient {
private static final String BASE_URL = "http://138.68.172.136:8001/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
retrofit=null;
if (retrofit==null) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}