import android.util.Log;
import net.technoscore.fixit.constants.AppConstants;
import net.technoscore.fixit.model.FXDeviceRegistrationResponse;
import net.technoscore.fixit.model.FXNormalLoginResponce;
import net.technoscore.fixit.model.FXProfessionalBusinessRegisterResponse;
import net.technoscore.fixit.model.FXRegistrationResponce;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitHelper {
private static RetrofitHelper ourInstance = new RetrofitHelper();
public static RetrofitHelper getInstance() {
return ourInstance;
}
private RetrofitHelper() {
}
RetrofitApi retrofitapi;
public void init() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).
connectTimeout(5, TimeUnit.MINUTES)
.readTimeout(5, TimeUnit.MINUTES)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(AppConstants.APP_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
retrofitapi = retrofit.create(RetrofitApi.class);
}
public void callNormalLogin(Callback<FXNormalLoginResponce> callback, String ltype
, String emailid, String password ) {
Call<FXNormalLoginResponce> response;
Log.d("userid", "userid** " + ltype);
Log.d("userid", "emailid** " + emailid);
Log.d("userid", "password** " + password);
response = retrofitapi.fixitNormalLogin(ltype, emailid, password);
response.enqueue(callback);
}
}
Example to hit service
public class CustomerSignInActivity extends AppCompatActivity implements
Callback<FXNormalLoginResponce> {
RippleView button_sign_up, button_customer_sign_in;
CustomFontTextView text_sign_up;
EditText enter_mail, enter_password, edtemail_id, edt_password;
RippleView rplSignIn;
RelativeLayout rel_sign_up;
AlertDialog dialog;
int currentApiVersion = android.os.Build.VERSION.SDK_INT;
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_client_activity);
inIt();
// Button Clicks
rplSignIn.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
@Override public void onComplete(RippleView rippleView) {
validations();
}
});
rel_sign_up.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),
CustomerSignUPActivity.class));
}
});
}
public void validations() {
dialog.show();
RetrofitHelper.getInstance().callNormalLogin
(this, "1", edtemail_id.getText().toString().trim()
, edt_password.getText().toString().trim());
}
@Override protected void onResume() {
super.onResume();
final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
if (currentApiVersion >= 19) {
getWindow().getDecorView().setSystemUiVisibility(flags);
decorView.setOnSystemUiVisibilityChangeListener
(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
decorView.setSystemUiVisibility(flags);
}
}
});
}
}
// initialization public void inIt() {
edtemail_id = (EditText) findViewById(R.id.edtemail_id);
edt_password = (EditText) findViewById(R.id.edt_password);
dialog = new SpotsDialog(CustomerSignInActivity.this, R.style.Custom);
rplSignIn = (RippleView) findViewById(R.id.rplSignIn);
rel_sign_up = (RelativeLayout) findViewById(R.id.rel_sign_up);
}
public boolean emailValidator(String email) {
Pattern pattern;
Matcher matcher;
final String EMAIL_PATTERN =
"^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
pattern = Pattern.compile(EMAIL_PATTERN);
matcher = pattern.matcher(email);
return matcher.matches();
}
@Override
public void onResponse(Call<FXNormalLoginResponce> call, Response<FXNormalLoginResponce>
response) {
dialog.dismiss();
ArrayList<FXNormalLoginData> data = response.body().getData();
Response_meta resmeta = response.body().getResponse_meta();
if (Integer.parseInt(resmeta.getCode()) == 200) {
SweetAlertDialog successDialog = new SweetAlertDialog
(this, SweetAlertDialog.SUCCESS_TYPE);
successDialog.getProgressHelper().setBarColor(Color.parseColor("#6CD3B5"));
successDialog.setTitleText(resmeta.getMessage());
successDialog.setCancelable(false);
successDialog.show();
successDialog.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
@Override public void onClick(SweetAlertDialog sDialog) {
sDialog.dismissWithAnimation();
startActivity(new Intent(getApplicationContext(), LandingScreen.class));
}
});
} else {
FXResponseErrors[] errors = response.body().getResponse_meta().getErrors();
final SweetAlertDialog errorDialog = new SweetAlertDialog
(this, SweetAlertDialog.ERROR_TYPE);
errorDialog.getProgressHelper().setBarColor(Color.parseColor("#6CD3B5"));
errorDialog.setTitleText(resmeta.getMessage());
if(data.size()>0)
{
errorDialog.setTitleText(data.get(0).getMessage());
}
else if(errors.length > 0 ) {
errorDialog.setTitleText(errors[0].getError());
}
else errorDialog.setTitleText(resmeta.getMessage());
errorDialog.setCancelable(false);
errorDialog.show();
errorDialog.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener()
{
@Override public void onClick(SweetAlertDialog sDialog) {
sDialog.dismissWithAnimation();
errorDialog.dismiss();
}
});
}
}
@Override public void onFailure(Call<FXNormalLoginResponce> call, Throwable t) {
dialog.dismiss();
}
}
Another way to implement Retrofit2
public class Singleton {
Context mContext;
private static Singleton ourInstance = new Singleton();
public static Singleton getInstance() {
return ourInstance;
}
private Singleton() {
}
private User mUser;
// prolincLogin public ApiService getApi() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor)
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS).build();
Retrofit retrofit = new Retrofit.Builder()
// .baseUrl("http://prolinc.in/prolincApi/")
.baseUrl("http://prolinc.in/prolinc_dev/prolincApi/")
.client(client)
//.baseUrl("http://192.168.1.87/prolinc/prolincApi/")
.addConverterFactory(GsonConverterFactory.create())
/* .addConverterFactory(GsonConverterFactory.create(gson))*/
.build();
return retrofit.create(ApiService.class);
}
}
Example to hit service
public void sendOTP() {
SharedPrefHelper sph = new SharedPrefHelper(this);
Gson gsn = new Gson();
String js = sph.getString("USERDATA", "");
UserData objUserData = gsn.fromJson(js, UserData.class);
String token = objUserData.getToken();
String UserID = objUserData.getUserId();
String phone = objUserData.getPhone();
mApiService = Singleton.getInstance().getApi();
mApiService.sedOTP(UserID, phone).enqueue
(new Callback<AbstractResponseList<ResponseMeta, OTPModel>>() {
@Override
public void onResponse(Response<AbstractResponseList<ResponseMeta, OTPModel>> response) {
ResponseMeta responseMeta = response.body().getResponseMeta();
List<OTPModel> data = response.body().getData();
if (responseMeta.getCode() == 200) {
Toast.makeText(OTPVerificationActivity.this,
data.get(0).getMessage(), Toast.LENGTH_SHORT).show();
} else {
}
}
@Override
public void onFailure(Throwable t) {
Toast.makeText(OTPVerificationActivity.this, "Try again!", Toast.LENGTH_SHORT).show();
}
});
}