Skip to content

Instantly share code, notes, and snippets.

@Kaushik1502
Last active September 1, 2016 11:11
Show Gist options
  • Save Kaushik1502/119172754c4d3c5e0e8847c57fdf934d to your computer and use it in GitHub Desktop.
Save Kaushik1502/119172754c4d3c5e0e8847c57fdf934d to your computer and use it in GitHub Desktop.
retrofit 2
/*GET request */
Call<ServicelistMain> call = RestService.getInstance().restInterface.getServiceList();
call.enqueue(new Callback<ServicelistMain>() {
@Override
public void onResponse(Call<ServicelistMain> call, Response<ServicelistMain> response) {
if (response.isSuccessful()) {
//if you showing progress dialog dismiss here
serviceList.clear();
serviceList.addAll(response.body().getServicelist());
adapter.notifyDataSetChanged();
}
}
@Override
public void onFailure(Call<ServicelistMain> call, Throwable t) {
//if you showing progress dialog dismiss here
//you can give retry option here
}
});
/*POST code */
progressDialog.setMessage(getResources().getString(R.string.submitting_otp));
progressDialog.show();
String email = emailEdtTxt.getText().toString().trim();
String password = passwordEdtTxt.getText().toString().trim();
String otp = otpEdtTxt.getText().toString().trim();
Call<OtpVerifier> call = RestService.getInstance().restInterface.verifyOtp(
email,password,otp);
call.enqueue(new Callback<OtpVerifier>() {
@Override
public void onResponse(Call<OtpVerifier> call, Response<OtpVerifier> response) {
dialog.dismiss();
if (progressDialog.isShowing())
progressDialog.dismiss();
if (response.isSuccessful()) {
int isSuccess = response.body().getIsSuccess();
if (isSuccess == 1) {
callLogInService();
} else {
//show api error msg here
}
}
}
@Override
public void onFailure(Call<OtpVerifier> call, Throwable t) {
if (progressDialog.isShowing())
progressDialog.dismiss();
}
});
/* Multipart Request */
String firstName = firstNameEdtTxt.getText().toString().trim();
String lastName = lastNameEdtTxt.getText().toString().trim();
String email = emailEdtTxt.getText().toString().trim();
String password = passwordEdtTxt.getText().toString().trim();
String phnNo = phnEdtTxt.getText().toString().trim();
String strAddress = addressEdtTxt.getText().toString().trim();
progressDialog.setMessage(getResources().getString(R.string.registering));
progressDialog.show();
RequestBody requestBodyFirstName = RequestBody.create(
MediaType.parse("text/plain"), firstName);
RequestBody requestBodyLastName = RequestBody.create(
MediaType.parse("text/plain"), lastName);
RequestBody requestBodyEmail = RequestBody.create(
MediaType.parse("text/plain"), email);
RequestBody requestBodyPassword = RequestBody.create(
MediaType.parse("text/plain"), password);
RequestBody requestBodyUserType = RequestBody.create(
MediaType.parse("text/plain"), IServiceCustomerApp.USER_TYPE);
RequestBody requestBodyDeviceType = RequestBody.create(
MediaType.parse("text/plain"), IServiceCustomerApp.DEVICE_TYPE);
RequestBody requestBodyToken = RequestBody.create(
MediaType.parse("text/plain"), token);
RequestBody requestBodyPhn = RequestBody.create(
MediaType.parse("text/plain"), phnNo);
RequestBody requestBodyAddress = RequestBody.create(
MediaType.parse("multipart/form-data"), strAddress);
RequestBody requestBodyLat = RequestBody.create(
MediaType.parse("multipart/form-data"), String.valueOf(latitude));
RequestBody requestBodyLng = RequestBody.create(
MediaType.parse("multipart/form-data"), String.valueOf(longitude));
/*RequestBody requestBodyOrderDataFile = RequestBody.create(
MediaType.parse("multipart/form-data"), file);*/
Call<Registration> call = RestService.getInstance().restInterface.
createUser(requestBodyFirstName,requestBodyLastName,
requestBodyEmail,requestBodyPassword,
requestBodyUserType,requestBodyPhn,
requestBodyDeviceType,requestBodyToken,
requestBodyAddress,requestBodyLat,
requestBodyLng,null);
call.enqueue(new Callback<Registration>() {
@Override
public void onResponse(Call<Registration> call, Response<Registration> response) {
if (progressDialog.isShowing())
progressDialog.dismiss();
if (response.isSuccessful()) {
int isSuccess = response.body().getIsSuccess();
if (isSuccess == 1) {
CustomPrefs.writeString(SignUpActivity.this,
IServiceCustomerApp.KEY_IS_VERIFIED,
response.body().getUserInfo().getVerifiedAccount());
openLogInActivity();
} else {
//show api error msg here
}
}
}
@Override
public void onFailure(Call<Registration> call, Throwable t) {
if (progressDialog.isShowing())
progressDialog.dismiss();
}
});
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'
public interface RestInterface {
String BASE_URL = "http://www.base.url/";
@Multipart
@POST("end_point_of_url")
Call<Registration> createUser(@Part("fname") RequestBody firstName,
@Part("lname") RequestBody lastName,
@Part("email") RequestBody email,
@Part("pwd") RequestBody password,
@Part("user_type") RequestBody userType,
@Part("ph") RequestBody phnNo,
@Part("device_type") RequestBody deviceType,
@Part("regid") RequestBody regId,
@Part("address") RequestBody address,
@Part("add_lat") RequestBody addressLat,
@Part("add_long") RequestBody addressLng,
@Part MultipartBody.Part image);
@FormUrlEncoded
@POST("end_point_of_url")
Call<OtpVerifier> verifyOtp(@Field("email") String email,
@Field("pwd") String password,
@Field("otp") String otp);
@GET("end_point_of_url")
Call<ServicelistMain> getServiceList();//if there is any query string then use @Query annotation you can use @PATH for dynamic value
}
/*Restservice class*/
public class RestService {
private static RestService restService;
public RestInterface restInterface;
private static final int CONNECT_TIMEOUT_MILLIS = 2 * 60; // 2 minute
private static final int READ_TIMEOUT_MILLIS = 2 * 60;
public RestService() {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
clientBuilder.addInterceptor(httpLoggingInterceptor);
}
restInterface = new Retrofit.Builder()
.baseUrl(RestInterface.BASE_URL)
.client(clientBuilder.retryOnConnectionFailure(true)
.readTimeout(READ_TIMEOUT_MILLIS, TimeUnit.SECONDS)
.connectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.SECONDS)
.build())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(RestInterface.class);
}
public static RestService getInstance() {
if (restService == null) {
restService = new RestService();
}
return restService;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment