STEP-1: Library
compile('com.squareup.retrofit2:retrofit:2.1.0') {
exclude module: 'okhttp'
}
STEP-2: API Client
public class APIClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String url) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
// OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
OkHttpClient client = new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60,TimeUnit.SECONDS).addInterceptor(interceptor).build();
retrofit = new Retrofit.Builder()
.baseUrl(url)//localhost
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit;
}
}
3. API Interface
@POST("api/email_verification.json")
Call<AddFolderResponse> emailVerification(@Body EmailVerificationRequest emailVerificationRequest);
@Multipart
@POST("api/set_profile_picture.json")
Call<AddFolderResponse> profileImageUpload(@Part MultipartBody.Part file, @Part("file[]") RequestBody id_proof, @Query("token") String token);
4. APP Call
public void callWebServiceForEmailVerification(){
String token = prefs.getString(CONSTANTS.LOGGED_TOKEN);
EmailVerificationRequest emailVerirequest = new EmailVerificationRequest();
emailVerirequest.token = token;
APIInterface apiInterface = APIClient.getClient(CONSTANTS.BASE_URL).create(APIInterface.class);
if(NetworkClass.getInstance().checkInternet(PaidSubscriberDashboardActivity.this) == true){
ProgressClass.getProgressInstance().showDialog(PaidSubscriberDashboardActivity.this);
Call<AddFolderResponse> call = apiInterface.emailVerification(emailVerirequest);
call.enqueue(new Callback<AddFolderResponse>() {
@Override
public void onResponse(Call<AddFolderResponse> call, Response<AddFolderResponse> response) {
ProgressClass.getProgressInstance().stopProgress();
try {
if (response.isSuccessful()) {
if (response.body().getMessage().getSuccess().equalsIgnoreCase("success")) {
showAlertMsg("Alert", "Verfication email sended. Check your mail and follow instruction");
// AlertDialogSingleClick.getInstance().showDialog(SubscriberDashboardActivity.this, "Email Alert", "Verfication email sended. Check your mail and follow instruction");
}
}
}catch (NullPointerException npe){
Log.e(TAG, "#Error : "+npe, npe);
showSnackBar();
}
}
@Override
public void onFailure(Call<AddFolderResponse> call, Throwable t) {
call.cancel();
Toast.makeText(PaidSubscriberDashboardActivity.this, "Something went wrong!", Toast.LENGTH_SHORT).show();
ProgressClass.getProgressInstance().stopProgress();
}
});
}else {
NetworkDialogHelper.getInstance().showDialog(PaidSubscriberDashboardActivity.this);
}
}