Android Retrofit - Call login service in all other service?
I'm building a client for a REST service. The service has a login service that generates a token.
The login service has the following format:
$.post('http://xxx.xxx.xxx.xxx/?json=true',{machineID: "fMUVxYdG1X3hWb7GNkTd", mail: "user@user.com", pass: "123", function: "dash"},function(d){
console.log(d.$user)
})
With the following response. auth_token is apiKey in this service.
{"ok":true,"auth_token":"078c302cecc90206fec20bc8306a93ba"}
So in my android app, I create an interface like
public interface RestService {
@POST("/")
Call<LoginResponse> login(@Query("json") boolean json, @Body Login login);
@GET("/{apiKey}/monitor/{groupKey}")
Call<List<Monitor>> getMonitors(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey);
@GET("/{apiKey}/monitor/{groupKey}/{monitorId}")
Call<Monitor> getMonitor(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey, @Path("monitorId") String monitorId);
@GET("/{apiKey}/videos/{groupKey}/{monitorId}")
Call<VideoObject> getMonitorVideos(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey, @Path("monitorId") String monitorId);
@GET("/{apiKey}/videos/{groupKey}")
Call<VideoObject> getVideos(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey, @QueryMap Map<String, String> options);
@GET("/{apiKey}/control/{groupKey}/{monitorId}/{action}")
Call<ResponseBody> control(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey, @Path("monitorId") String monitorId, @Path("action") String action);
}
I have another class that initiates the service.
public void init(String host, int port, boolean ssl) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
baseUrl = String.format(Locale.getDefault(), "%s://%s:%d", (ssl ? HTTPS : HTTP), host, port);
okHttpClient = new OkHttpClient().newBuilder().addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
return chain.proceed(originalRequest);
}
})
.addInterceptor(logging)
.build();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(JacksonConverterFactory.create(mapper))
.client(okHttpClient)
.build();
restService = retrofit.create(RestService.class);
this.host = host;
this.port = port;
this.ssl = ssl;
}
Here is the login service and another service.
public void login(final Login login, final Callback<LoginResponse> callback) {
Call<LoginResponse> call = restService.login(true, login);
call.enqueue(callback);
}
public void getMonitors(Callback<List<Monitor>> callback) {
Call<List<Monitor>> call = restService.getMonitors(apiKey, groupKey);
call.enqueue(callback);
}
However, I want to be able to call login service on each of the other services and upon successful response, then I will call the actual service.
Anyway I can do this with retrofit?
Appreciate any feedback.
android rest retrofit
add a comment |
I'm building a client for a REST service. The service has a login service that generates a token.
The login service has the following format:
$.post('http://xxx.xxx.xxx.xxx/?json=true',{machineID: "fMUVxYdG1X3hWb7GNkTd", mail: "user@user.com", pass: "123", function: "dash"},function(d){
console.log(d.$user)
})
With the following response. auth_token is apiKey in this service.
{"ok":true,"auth_token":"078c302cecc90206fec20bc8306a93ba"}
So in my android app, I create an interface like
public interface RestService {
@POST("/")
Call<LoginResponse> login(@Query("json") boolean json, @Body Login login);
@GET("/{apiKey}/monitor/{groupKey}")
Call<List<Monitor>> getMonitors(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey);
@GET("/{apiKey}/monitor/{groupKey}/{monitorId}")
Call<Monitor> getMonitor(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey, @Path("monitorId") String monitorId);
@GET("/{apiKey}/videos/{groupKey}/{monitorId}")
Call<VideoObject> getMonitorVideos(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey, @Path("monitorId") String monitorId);
@GET("/{apiKey}/videos/{groupKey}")
Call<VideoObject> getVideos(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey, @QueryMap Map<String, String> options);
@GET("/{apiKey}/control/{groupKey}/{monitorId}/{action}")
Call<ResponseBody> control(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey, @Path("monitorId") String monitorId, @Path("action") String action);
}
I have another class that initiates the service.
public void init(String host, int port, boolean ssl) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
baseUrl = String.format(Locale.getDefault(), "%s://%s:%d", (ssl ? HTTPS : HTTP), host, port);
okHttpClient = new OkHttpClient().newBuilder().addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
return chain.proceed(originalRequest);
}
})
.addInterceptor(logging)
.build();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(JacksonConverterFactory.create(mapper))
.client(okHttpClient)
.build();
restService = retrofit.create(RestService.class);
this.host = host;
this.port = port;
this.ssl = ssl;
}
Here is the login service and another service.
public void login(final Login login, final Callback<LoginResponse> callback) {
Call<LoginResponse> call = restService.login(true, login);
call.enqueue(callback);
}
public void getMonitors(Callback<List<Monitor>> callback) {
Call<List<Monitor>> call = restService.getMonitors(apiKey, groupKey);
call.enqueue(callback);
}
However, I want to be able to call login service on each of the other services and upon successful response, then I will call the actual service.
Anyway I can do this with retrofit?
Appreciate any feedback.
android rest retrofit
Write a method that callslogin
and if that's successful then callsgetMonitors
. Write another method that callslogin
and if that's successful callsgetWhateverAnotherServiceMethodIsCalled
. Repeat as necessary and name these methods whatever makes sense for you. I'm not clear on what the problem is.
– nasch
2 days ago
add a comment |
I'm building a client for a REST service. The service has a login service that generates a token.
The login service has the following format:
$.post('http://xxx.xxx.xxx.xxx/?json=true',{machineID: "fMUVxYdG1X3hWb7GNkTd", mail: "user@user.com", pass: "123", function: "dash"},function(d){
console.log(d.$user)
})
With the following response. auth_token is apiKey in this service.
{"ok":true,"auth_token":"078c302cecc90206fec20bc8306a93ba"}
So in my android app, I create an interface like
public interface RestService {
@POST("/")
Call<LoginResponse> login(@Query("json") boolean json, @Body Login login);
@GET("/{apiKey}/monitor/{groupKey}")
Call<List<Monitor>> getMonitors(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey);
@GET("/{apiKey}/monitor/{groupKey}/{monitorId}")
Call<Monitor> getMonitor(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey, @Path("monitorId") String monitorId);
@GET("/{apiKey}/videos/{groupKey}/{monitorId}")
Call<VideoObject> getMonitorVideos(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey, @Path("monitorId") String monitorId);
@GET("/{apiKey}/videos/{groupKey}")
Call<VideoObject> getVideos(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey, @QueryMap Map<String, String> options);
@GET("/{apiKey}/control/{groupKey}/{monitorId}/{action}")
Call<ResponseBody> control(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey, @Path("monitorId") String monitorId, @Path("action") String action);
}
I have another class that initiates the service.
public void init(String host, int port, boolean ssl) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
baseUrl = String.format(Locale.getDefault(), "%s://%s:%d", (ssl ? HTTPS : HTTP), host, port);
okHttpClient = new OkHttpClient().newBuilder().addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
return chain.proceed(originalRequest);
}
})
.addInterceptor(logging)
.build();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(JacksonConverterFactory.create(mapper))
.client(okHttpClient)
.build();
restService = retrofit.create(RestService.class);
this.host = host;
this.port = port;
this.ssl = ssl;
}
Here is the login service and another service.
public void login(final Login login, final Callback<LoginResponse> callback) {
Call<LoginResponse> call = restService.login(true, login);
call.enqueue(callback);
}
public void getMonitors(Callback<List<Monitor>> callback) {
Call<List<Monitor>> call = restService.getMonitors(apiKey, groupKey);
call.enqueue(callback);
}
However, I want to be able to call login service on each of the other services and upon successful response, then I will call the actual service.
Anyway I can do this with retrofit?
Appreciate any feedback.
android rest retrofit
I'm building a client for a REST service. The service has a login service that generates a token.
The login service has the following format:
$.post('http://xxx.xxx.xxx.xxx/?json=true',{machineID: "fMUVxYdG1X3hWb7GNkTd", mail: "user@user.com", pass: "123", function: "dash"},function(d){
console.log(d.$user)
})
With the following response. auth_token is apiKey in this service.
{"ok":true,"auth_token":"078c302cecc90206fec20bc8306a93ba"}
So in my android app, I create an interface like
public interface RestService {
@POST("/")
Call<LoginResponse> login(@Query("json") boolean json, @Body Login login);
@GET("/{apiKey}/monitor/{groupKey}")
Call<List<Monitor>> getMonitors(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey);
@GET("/{apiKey}/monitor/{groupKey}/{monitorId}")
Call<Monitor> getMonitor(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey, @Path("monitorId") String monitorId);
@GET("/{apiKey}/videos/{groupKey}/{monitorId}")
Call<VideoObject> getMonitorVideos(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey, @Path("monitorId") String monitorId);
@GET("/{apiKey}/videos/{groupKey}")
Call<VideoObject> getVideos(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey, @QueryMap Map<String, String> options);
@GET("/{apiKey}/control/{groupKey}/{monitorId}/{action}")
Call<ResponseBody> control(@Path("apiKey") String apiKey, @Path("groupKey") String groupKey, @Path("monitorId") String monitorId, @Path("action") String action);
}
I have another class that initiates the service.
public void init(String host, int port, boolean ssl) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
baseUrl = String.format(Locale.getDefault(), "%s://%s:%d", (ssl ? HTTPS : HTTP), host, port);
okHttpClient = new OkHttpClient().newBuilder().addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
return chain.proceed(originalRequest);
}
})
.addInterceptor(logging)
.build();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(JacksonConverterFactory.create(mapper))
.client(okHttpClient)
.build();
restService = retrofit.create(RestService.class);
this.host = host;
this.port = port;
this.ssl = ssl;
}
Here is the login service and another service.
public void login(final Login login, final Callback<LoginResponse> callback) {
Call<LoginResponse> call = restService.login(true, login);
call.enqueue(callback);
}
public void getMonitors(Callback<List<Monitor>> callback) {
Call<List<Monitor>> call = restService.getMonitors(apiKey, groupKey);
call.enqueue(callback);
}
However, I want to be able to call login service on each of the other services and upon successful response, then I will call the actual service.
Anyway I can do this with retrofit?
Appreciate any feedback.
android rest retrofit
android rest retrofit
asked 2 days ago
ankank
6321829
6321829
Write a method that callslogin
and if that's successful then callsgetMonitors
. Write another method that callslogin
and if that's successful callsgetWhateverAnotherServiceMethodIsCalled
. Repeat as necessary and name these methods whatever makes sense for you. I'm not clear on what the problem is.
– nasch
2 days ago
add a comment |
Write a method that callslogin
and if that's successful then callsgetMonitors
. Write another method that callslogin
and if that's successful callsgetWhateverAnotherServiceMethodIsCalled
. Repeat as necessary and name these methods whatever makes sense for you. I'm not clear on what the problem is.
– nasch
2 days ago
Write a method that calls
login
and if that's successful then calls getMonitors
. Write another method that calls login
and if that's successful calls getWhateverAnotherServiceMethodIsCalled
. Repeat as necessary and name these methods whatever makes sense for you. I'm not clear on what the problem is.– nasch
2 days ago
Write a method that calls
login
and if that's successful then calls getMonitors
. Write another method that calls login
and if that's successful calls getWhateverAnotherServiceMethodIsCalled
. Repeat as necessary and name these methods whatever makes sense for you. I'm not clear on what the problem is.– nasch
2 days ago
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54252872%2fandroid-retrofit-call-login-service-in-all-other-service%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54252872%2fandroid-retrofit-call-login-service-in-all-other-service%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Write a method that calls
login
and if that's successful then callsgetMonitors
. Write another method that callslogin
and if that's successful callsgetWhateverAnotherServiceMethodIsCalled
. Repeat as necessary and name these methods whatever makes sense for you. I'm not clear on what the problem is.– nasch
2 days ago