Skip to content

Instantly share code, notes, and snippets.

@arthtilva
Last active September 3, 2020 13:42
Show Gist options
  • Save arthtilva/ad644b7108e3dc61818f2aeb3db931b2 to your computer and use it in GitHub Desktop.
Save arthtilva/ad644b7108e3dc61818f2aeb3db931b2 to your computer and use it in GitHub Desktop.
Retrofit helper class
import android.accounts.NetworkErrorException;
import android.util.Log;
import com.caregiver.BuildConfig;
import java.io.IOException;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by arthtilva on 10-May-17.
*/
public class RetrofitHelper {
private API gsonAPI;
private ConnectionCallBack connectionCallBack;
public RetrofitHelper() {
OkHttpClient okHttpClient = new OkHttpClient();
int TIMEOUT = 2 * 60 * 1000;
Retrofit gsonretrofit = new Retrofit.Builder()
.baseUrl(BuildConfig.SERVER_URL)
.client(okHttpClient.newBuilder().connectTimeout(TIMEOUT, TimeUnit.SECONDS).readTimeout(TIMEOUT, TimeUnit.SECONDS).writeTimeout(TIMEOUT, TimeUnit.SECONDS).build())
.addConverterFactory(GsonConverterFactory.create())
.build();
gsonAPI = gsonretrofit.create(API.class);
}
public API api() {
return gsonAPI;
}
public void callApi(Call<ResponseBody> call, ConnectionCallBack callBack) {
connectionCallBack = callBack;
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.code() == 200) {
if (connectionCallBack != null)
connectionCallBack.onSuccess(response);
} else {
try {
String res = response.body().string();
if (connectionCallBack != null)
connectionCallBack.onError(response.code(), res);
} catch (IOException | NullPointerException e) {
Log.i("TAG", "onResponse: " + call.request().url());
e.printStackTrace();
if (connectionCallBack != null)
connectionCallBack.onError(response.code(), e.getMessage());
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable error) {
String message = null;
if (error instanceof NetworkErrorException) {
message = "Please check your internet connection";
} else if (error instanceof ParseException) {
message = "Parsing error! Please try again after some time!!";
} else if (error instanceof TimeoutException) {
message = "Connection TimeOut! Please check your internet connection.";
} else if (error instanceof UnknownHostException) {
message = "Please check your internet connection and try later";
} else if (error instanceof Exception) {
message = error.getMessage();
}
if (connectionCallBack != null)
connectionCallBack.onError(-1, message);
}
});
}
public interface ConnectionCallBack {
public void onSuccess(Response<ResponseBody> body);
public void onError(int code, String error);
}
}
/******************USAGE***********************/
private void getAPIData() {
Utils.showProgress(activity);
RetrofitHelper retrofitHelper = new RetrofitHelper();
Call<ResponseBody> call;
call = retrofitHelper.api().login();
retrofitHelper.callApi(call, new RetrofitHelper.ConnectionCallBack() {
@Override
public void onSuccess(Response<ResponseBody> body) {
Utils.dismissProgress();
try {
if (body.code() != 200) {
Utils.serverError(activity, body.code());
return;
}
String response = body.body().string();
Log.i("TAG", "onSuccess: " + response);
} catch (IOException | NullPointerException | JsonSyntaxException | JSONException e) {
e.printStackTrace();
}
}
@Override
public void onError(int code, String error) {
Utils.dismissProgress();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment