Dependencies
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.6.1'
// additional things
implementation 'com.squareup.okhttp3:logging-interceptor:4.0.1'
implementation 'com.squareup.okhttp3:okhttp:4.0.1'
Need to create classes
ApiInterface.java
public interface ApiInterface {
@FormUrlEncoded
@POST("get-token")
Call<ResponseBody> getToken(@FieldMap Map<String, String> fieldMap);
}
ApiClient.java
public class ApiClient {
private static Retrofit retrofit;
private static Retrofit retrofitPlaid;
public static Retrofit getClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.readTimeout(60, TimeUnit.MINUTES)
.writeTimeout(60, TimeUnit.MINUTES)
.build();
retrofit = new Retrofit.Builder()
.baseUrl("service url")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit;
}
}
Implement in Activity or Service
# Create apiinterface object
ApiInterface apiService;
apiService = ApiClient.getClient().create(ApiInterface.class);
#call api (Normal)
Map<String, String> map = new HashMap<String, String>();
map.put("request_param_name", "1");Call<ResponseBody> call= apiService.dashboard(AppSharedPreferences.getSharePreference(mContext).getAccessToken(), map);
#Response
call.clone().enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
if (response.isSuccessful()) {
if (response.body().getSuccess() == 1) {
Toast.makeText(mContext, response.body().getData().getMessage(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(mContext, response.body().getError().get(0), Toast.LENGTH_SHORT).show();
}
} else {
assert response.body() != null;
Toast.makeText(mContext, response.body().getError().get(0), Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
}
#call api (Image with text)
@Multipart
@POST("users/edit_profile")
Call<ResponseBody> editProfile(@PartMap Map<String, RequestBody> fieldMap, @Part MultipartBody.Part image);
// creaet param
MultipartBody.Part body = null;
if (imageFile != null) {
RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), imageFile);
body = MultipartBody.Part.createFormData("image", imageFile.getName(), reqFile);
}
Map<String, RequestBody> map = new HashMap<>();
map.put(Constant.TAG_USER_ID, toRequestBody(user.user_id));
Call<ResponseBody> callApi = apiService.editProfile(map, body);
editProfileHttpUrl = callApi.request().url();
callApi.clone().enqueue(this);
public static RequestBody toRequestBody(String value) {
RequestBody body = RequestBody.create(MediaType.parse("text/plain"), value);
return body;
}
#call api (only Image)
@Multipart
@POST("edit-profile")
Call<UserData> uploadProfile(@Header("Authorization") String auth, @Part MultipartBody.Part image);
Request
MultipartBody.Part body = null;
if (imageFile != null) {
RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), imageFile);
body = MultipartBody.Part.createFormData("profile_pic", imageFile.getName(), reqFile);
}
Call<UserData> callUploadProfile = apiService.uploadProfile(AppSharedPreferences.getSharePreference(mContext).getAccessToken(), body);