Skip to content

Instantly share code, notes, and snippets.

@IevgenOsetrov
Last active April 4, 2017 08:27
Show Gist options
  • Save IevgenOsetrov/f405224950cbcc82415d2f3506dff0c9 to your computer and use it in GitHub Desktop.
Save IevgenOsetrov/f405224950cbcc82415d2f3506dff0c9 to your computer and use it in GitHub Desktop.
Retrofit singleton
LoginService loginService = RetrofitApi.getInstance().getLoginService();
SendUser sendUser = new SendUser(loginEditText.getText().toString(), passwordEditText.getText().toString());
Call<User> userCall = loginService.getUser(sendUser);
userCall.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if (response.isSuccessful()) {
} else {
Log.d(TAG, "Response error " + response.raw().toString());
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
Log.d(TAG, "Error " + t.getMessage());
}
});
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import ua.net.lsoft.casinoprovision.server.interfaces.DealersService;
import ua.net.lsoft.casinoprovision.server.interfaces.LoginService;
import ua.net.lsoft.casinoprovision.server.interfaces.PlayersService;
public class RetrofitApi {
private static RetrofitApi instance;
public static final String BASE_URL = "";
private LoginService loginService;
public static RetrofitApi getInstance() {
if (instance == null) {
instance = new RetrofitApi();
}
return instance;
}
private RetrofitApi() {
buildRetrofit(BASE_URL);
}
private void buildRetrofit(String url) {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
loginService = retrofit.create(LoginService.class);
}
public LoginService getLoginService() {
return loginService;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment