Skip to content

Instantly share code, notes, and snippets.

@ankuryadav7
Last active June 22, 2018 17:54
Show Gist options
  • Save ankuryadav7/89210fe34248cc127ffb3f84d4479df4 to your computer and use it in GitHub Desktop.
Save ankuryadav7/89210fe34248cc127ffb3f84d4479df4 to your computer and use it in GitHub Desktop.
Add below files in Gradle
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.6.0'
Create Api Client
public class ApiClient {
private static Retrofit retrofit = null;
private static final String CACHE_CONTROL = "Cache-Control";
public static Retrofit getClient() {
if (retrofit == null) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
httpClient.readTimeout(120, TimeUnit.SECONDS);
httpClient.connectTimeout(120, TimeUnit.SECONDS);
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();
retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(httpClient.build())
.build();
}
return retrofit;
}
}
Create End Point
public interface Endpoint{
@FormUrlEncoded
@POST("api/auth/login")
Call<LoginBeann> loginCall(@Field("email") String email, @Field("password") String password, @Field("lang")String language);
}
LoginBean Class--
public class LoginBeann{
@SerializedName("status")
@Expose
private String status;
@SerializedName("message")
@Expose
private String message;
@SerializedName("data")
@Expose
private LoginBean data;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public LoginBean getData() {
return data;
}
public void setData(LoginBean data) {
this.data = data;
}
}
After Above setup. We can call the Login API
Endpoint endpoint = ApiClient.getClient().create(Endpoint.class);
private Call<LoginBeann> loginCall;
loginCall = endpoint.loginCall(email,password,language);
loginCall.enqueue(new Callback<LoginBeann>() {
@Override
public void onResponse(Call<LoginBeann> call, Response<LoginBeann> response) {
if (dialog.isShowing()) {
dialog.dismiss();
}
if (response.isSuccessful() && response.body().getData() != null && response.body().getStatus().equals("success")) {
}else if (response.body() !=null && response.body().getStatus().equals("fail")) {
toastCall(response.body().getMessage());
} else {
toastCall(response.body().getMessage());
}
}
@Override
public void onFailure(Call<LoginBeann> call, Throwable t) {
if (dialog.isShowing()) {
dialog.dismiss();
}
if (call.isCanceled())
return;
new PrintLog(activity).display("Login Error",t+"");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment