Created
January 17, 2020 09:13
-
-
Save JaldeepAsodariya/ca576fe0b2062e7df35edf0349d0c546 to your computer and use it in GitHub Desktop.
Retrofit builder client sample with implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import java.util.concurrent.TimeUnit; | |
import okhttp3.OkHttpClient; | |
import okhttp3.logging.HttpLoggingInterceptor; | |
import retrofit2.Retrofit; | |
import retrofit2.converter.gson.GsonConverterFactory; | |
public class APIRetroBuilder { | |
private static Retrofit OBJ_RETROFIT = null; | |
private static String BASE_URL = "https://www.example.com/api/"; | |
public static Retrofit getRetroBuilder(Context context, boolean enableConnectTimeout) { | |
if (OBJ_RETROFIT == null) { | |
if (BuildConfig.BUILD_TYPE.equals(AppConstants.DEBUG_BUILD_TYPE)) { | |
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); | |
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); | |
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); | |
clientBuilder.addInterceptor(loggingInterceptor); // print OkHTTP log | |
if(enableConnectTimeout) { | |
clientBuilder.connectTimeout(2, TimeUnit.MINUTES); | |
clientBuilder.readTimeout(2, TimeUnit.MINUTES); | |
} | |
OBJ_RETROFIT = new Retrofit.Builder() | |
.baseUrl(BASE_URL) | |
.client(clientBuilder.build()) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build(); | |
} else { | |
OBJ_RETROFIT = new Retrofit.Builder() | |
.baseUrl(BASE_URL) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build(); | |
} | |
} | |
return OBJ_RETROFIT; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface APISample { | |
@GET("apiSampleList") | |
Call<ModelResponse> callSampleListApi(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
... | |
implementation 'com.squareup.retrofit2:retrofit:2.5.0' | |
implementation 'com.squareup.retrofit2:converter-gson:2.5.0' | |
implementation 'com.squareup.okhttp3:okhttp:3.2.0' | |
implementation 'com.squareup.okhttp3:logging-interceptor:3.3.1' // For print OkHTTP log | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
private void callGetSampleListAPI() { | |
progressbar.setVisibility(View.VISIBLE); | |
APISample objApiSample = APIRetroBuilder.getRetroBuilder(this, false).create(APISample.class); | |
Call<ModelResponse> callModelSampleRes = objApiSample.callSampleListApi(); | |
callModelSampleRes.enqueue(new Callback<ModelResponse>() { | |
@Override | |
public void onResponse(Call<ModelResponse> call, Response<ModelResponse> response) { | |
progressbar.setVisibility(View.GONE); | |
try { | |
ModelResponse objModelRes = response.body(); | |
if(objModelRes.getSuccess().equalsIgnoreCase("True")) { | |
// Update UI based on response | |
} else | |
Snackbar.make(mCrdntrlyot, objModelRes.getMessage(), Snackbar.LENGTH_LONG).show(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
Snackbar.make(mCrdntrlyot, "There are some internal problem", Snackbar.LENGTH_LONG).show(); | |
} | |
} | |
@Override | |
public void onFailure(Call<ModelResponse> call, Throwable t) { | |
progressbar.setVisibility(View.GONE); | |
Snackbar.make(mCrdntrlyot, "There are some server problem", Snackbar.LENGTH_LONG).show(); | |
} | |
}); | |
} | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment