Retrofit2 doesn't consume endpoint - Spotify API
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to use retrofit2 with Observables and Lombok for my models to get a token for the spotify API. I tried with postman and it worked correctly but I have problems with Java. I have these 3 classes:
AuthorizationServiceCommandImpl.java
import com.service.userservice.feature.authorization.backend.model.Token;
import lombok.NonNull;
import okhttp3.OkHttpClient;
import org.springframework.stereotype.Service;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Single;
import rx.schedulers.Schedulers;
@Service
public class AuthorizationServiceCommandImpl implements AuthorizationServiceCommand {
private AuthorizationService authorizationService;
private static OkHttpClient createOkHttpClient() {
final OkHttpClient.Builder httpClient =
new OkHttpClient.Builder();
return httpClient.build()
}
public void getToken(String code) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://accounts.spotify.com/api/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(createOkHttpClient())
.build();
authorizationService = retrofit.create(AuthorizationService.class);
// At this point all these attributes have a value, I used System.out.println to confirm:
// clientId, clientSecret, grantType, code, redirectUri
authorizationService
.getToken(clientId, clientSecret, grantType, code, redirectUri)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.map(token-> token.toString());
}
}
AuthorizationService.java
import com.service.userservice.feature.authorization.backend.model.Token;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
import rx.Single;
public interface AuthorizationService {
@FormUrlEncoded
@POST("token")
Single<Token> getToken(@Field("client_id") String clientId,
@Field("client_secret") String clientSecret,
@Field("grant_type") String grantType,
@Field("code") String code,
@Field("redirect_uri") String redirectUri);
}
My Model, Token.java
import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@SuppressWarnings("PMD.UnusedPrivateField")
public class Token {
@SerializedName("access_token")
private String accessToken;
@SerializedName("token_type")
private String tokenType;
@SerializedName("expires_in")
private String expiresIn;
@SerializedName("refresh_token")
private String refreshToken;
@SerializedName("scope")
private String scope;
@SerializedName("error_description")
private String errorDescription;
@SerializedName("error")
private String error;
@Override
public String toString() {
System.out.println("Token{" +
"accessToken='" + accessToken + ''' +
", tokenType='" + tokenType + ''' +
", expiresIn='" + expiresIn + ''' +
", refreshToken='" + refreshToken + ''' +
", scope='" + scope + ''' +
", errorDescription='" + errorDescription + ''' +
", error='" + error + ''' +
'}');
return "Token{" +
"accessToken='" + accessToken + ''' +
", tokenType='" + tokenType + ''' +
", expiresIn='" + expiresIn + ''' +
", refreshToken='" + refreshToken + ''' +
", scope='" + scope + ''' +
", errorDescription='" + errorDescription + ''' +
", error='" + error + ''' +
'}';
}
}
This line doesn't show anything:
.map(token-> token.toString());
It's like the call was never done. Not sure how I can confirm that the call was done and also I tried to remove these 2 Observables lines
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
and return
Single<Token> getToken(@Field("client_id") String clientId,
but not worked. Any idea? Thanks in advance
retrofit rx-java
add a comment |
I'm trying to use retrofit2 with Observables and Lombok for my models to get a token for the spotify API. I tried with postman and it worked correctly but I have problems with Java. I have these 3 classes:
AuthorizationServiceCommandImpl.java
import com.service.userservice.feature.authorization.backend.model.Token;
import lombok.NonNull;
import okhttp3.OkHttpClient;
import org.springframework.stereotype.Service;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Single;
import rx.schedulers.Schedulers;
@Service
public class AuthorizationServiceCommandImpl implements AuthorizationServiceCommand {
private AuthorizationService authorizationService;
private static OkHttpClient createOkHttpClient() {
final OkHttpClient.Builder httpClient =
new OkHttpClient.Builder();
return httpClient.build()
}
public void getToken(String code) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://accounts.spotify.com/api/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(createOkHttpClient())
.build();
authorizationService = retrofit.create(AuthorizationService.class);
// At this point all these attributes have a value, I used System.out.println to confirm:
// clientId, clientSecret, grantType, code, redirectUri
authorizationService
.getToken(clientId, clientSecret, grantType, code, redirectUri)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.map(token-> token.toString());
}
}
AuthorizationService.java
import com.service.userservice.feature.authorization.backend.model.Token;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
import rx.Single;
public interface AuthorizationService {
@FormUrlEncoded
@POST("token")
Single<Token> getToken(@Field("client_id") String clientId,
@Field("client_secret") String clientSecret,
@Field("grant_type") String grantType,
@Field("code") String code,
@Field("redirect_uri") String redirectUri);
}
My Model, Token.java
import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@SuppressWarnings("PMD.UnusedPrivateField")
public class Token {
@SerializedName("access_token")
private String accessToken;
@SerializedName("token_type")
private String tokenType;
@SerializedName("expires_in")
private String expiresIn;
@SerializedName("refresh_token")
private String refreshToken;
@SerializedName("scope")
private String scope;
@SerializedName("error_description")
private String errorDescription;
@SerializedName("error")
private String error;
@Override
public String toString() {
System.out.println("Token{" +
"accessToken='" + accessToken + ''' +
", tokenType='" + tokenType + ''' +
", expiresIn='" + expiresIn + ''' +
", refreshToken='" + refreshToken + ''' +
", scope='" + scope + ''' +
", errorDescription='" + errorDescription + ''' +
", error='" + error + ''' +
'}');
return "Token{" +
"accessToken='" + accessToken + ''' +
", tokenType='" + tokenType + ''' +
", expiresIn='" + expiresIn + ''' +
", refreshToken='" + refreshToken + ''' +
", scope='" + scope + ''' +
", errorDescription='" + errorDescription + ''' +
", error='" + error + ''' +
'}';
}
}
This line doesn't show anything:
.map(token-> token.toString());
It's like the call was never done. Not sure how I can confirm that the call was done and also I tried to remove these 2 Observables lines
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
and return
Single<Token> getToken(@Field("client_id") String clientId,
but not worked. Any idea? Thanks in advance
retrofit rx-java
add a comment |
I'm trying to use retrofit2 with Observables and Lombok for my models to get a token for the spotify API. I tried with postman and it worked correctly but I have problems with Java. I have these 3 classes:
AuthorizationServiceCommandImpl.java
import com.service.userservice.feature.authorization.backend.model.Token;
import lombok.NonNull;
import okhttp3.OkHttpClient;
import org.springframework.stereotype.Service;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Single;
import rx.schedulers.Schedulers;
@Service
public class AuthorizationServiceCommandImpl implements AuthorizationServiceCommand {
private AuthorizationService authorizationService;
private static OkHttpClient createOkHttpClient() {
final OkHttpClient.Builder httpClient =
new OkHttpClient.Builder();
return httpClient.build()
}
public void getToken(String code) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://accounts.spotify.com/api/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(createOkHttpClient())
.build();
authorizationService = retrofit.create(AuthorizationService.class);
// At this point all these attributes have a value, I used System.out.println to confirm:
// clientId, clientSecret, grantType, code, redirectUri
authorizationService
.getToken(clientId, clientSecret, grantType, code, redirectUri)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.map(token-> token.toString());
}
}
AuthorizationService.java
import com.service.userservice.feature.authorization.backend.model.Token;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
import rx.Single;
public interface AuthorizationService {
@FormUrlEncoded
@POST("token")
Single<Token> getToken(@Field("client_id") String clientId,
@Field("client_secret") String clientSecret,
@Field("grant_type") String grantType,
@Field("code") String code,
@Field("redirect_uri") String redirectUri);
}
My Model, Token.java
import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@SuppressWarnings("PMD.UnusedPrivateField")
public class Token {
@SerializedName("access_token")
private String accessToken;
@SerializedName("token_type")
private String tokenType;
@SerializedName("expires_in")
private String expiresIn;
@SerializedName("refresh_token")
private String refreshToken;
@SerializedName("scope")
private String scope;
@SerializedName("error_description")
private String errorDescription;
@SerializedName("error")
private String error;
@Override
public String toString() {
System.out.println("Token{" +
"accessToken='" + accessToken + ''' +
", tokenType='" + tokenType + ''' +
", expiresIn='" + expiresIn + ''' +
", refreshToken='" + refreshToken + ''' +
", scope='" + scope + ''' +
", errorDescription='" + errorDescription + ''' +
", error='" + error + ''' +
'}');
return "Token{" +
"accessToken='" + accessToken + ''' +
", tokenType='" + tokenType + ''' +
", expiresIn='" + expiresIn + ''' +
", refreshToken='" + refreshToken + ''' +
", scope='" + scope + ''' +
", errorDescription='" + errorDescription + ''' +
", error='" + error + ''' +
'}';
}
}
This line doesn't show anything:
.map(token-> token.toString());
It's like the call was never done. Not sure how I can confirm that the call was done and also I tried to remove these 2 Observables lines
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
and return
Single<Token> getToken(@Field("client_id") String clientId,
but not worked. Any idea? Thanks in advance
retrofit rx-java
I'm trying to use retrofit2 with Observables and Lombok for my models to get a token for the spotify API. I tried with postman and it worked correctly but I have problems with Java. I have these 3 classes:
AuthorizationServiceCommandImpl.java
import com.service.userservice.feature.authorization.backend.model.Token;
import lombok.NonNull;
import okhttp3.OkHttpClient;
import org.springframework.stereotype.Service;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Single;
import rx.schedulers.Schedulers;
@Service
public class AuthorizationServiceCommandImpl implements AuthorizationServiceCommand {
private AuthorizationService authorizationService;
private static OkHttpClient createOkHttpClient() {
final OkHttpClient.Builder httpClient =
new OkHttpClient.Builder();
return httpClient.build()
}
public void getToken(String code) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://accounts.spotify.com/api/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(createOkHttpClient())
.build();
authorizationService = retrofit.create(AuthorizationService.class);
// At this point all these attributes have a value, I used System.out.println to confirm:
// clientId, clientSecret, grantType, code, redirectUri
authorizationService
.getToken(clientId, clientSecret, grantType, code, redirectUri)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.map(token-> token.toString());
}
}
AuthorizationService.java
import com.service.userservice.feature.authorization.backend.model.Token;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
import rx.Single;
public interface AuthorizationService {
@FormUrlEncoded
@POST("token")
Single<Token> getToken(@Field("client_id") String clientId,
@Field("client_secret") String clientSecret,
@Field("grant_type") String grantType,
@Field("code") String code,
@Field("redirect_uri") String redirectUri);
}
My Model, Token.java
import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@SuppressWarnings("PMD.UnusedPrivateField")
public class Token {
@SerializedName("access_token")
private String accessToken;
@SerializedName("token_type")
private String tokenType;
@SerializedName("expires_in")
private String expiresIn;
@SerializedName("refresh_token")
private String refreshToken;
@SerializedName("scope")
private String scope;
@SerializedName("error_description")
private String errorDescription;
@SerializedName("error")
private String error;
@Override
public String toString() {
System.out.println("Token{" +
"accessToken='" + accessToken + ''' +
", tokenType='" + tokenType + ''' +
", expiresIn='" + expiresIn + ''' +
", refreshToken='" + refreshToken + ''' +
", scope='" + scope + ''' +
", errorDescription='" + errorDescription + ''' +
", error='" + error + ''' +
'}');
return "Token{" +
"accessToken='" + accessToken + ''' +
", tokenType='" + tokenType + ''' +
", expiresIn='" + expiresIn + ''' +
", refreshToken='" + refreshToken + ''' +
", scope='" + scope + ''' +
", errorDescription='" + errorDescription + ''' +
", error='" + error + ''' +
'}';
}
}
This line doesn't show anything:
.map(token-> token.toString());
It's like the call was never done. Not sure how I can confirm that the call was done and also I tried to remove these 2 Observables lines
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
and return
Single<Token> getToken(@Field("client_id") String clientId,
but not worked. Any idea? Thanks in advance
retrofit rx-java
retrofit rx-java
asked Nov 23 '18 at 19:21
DavidDavid
74211
74211
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The code:
.map(token->token.toString());
does do something: it returns a modified observable. However, the modified observable is then thrown away. An observable has to be subscribed in order to activate it.
It's possible you can get away with adding the operator at the end:
.map(token->token.toString())
.subscriber();
However, it isn't clear what you want to do with the mapped string.
Yes, that worked. I didn't want to do anything with the string, I just wanted to show on console if the call was done and if the token model was mapped correctly. Thanks for your help
– David
Nov 23 '18 at 23:11
add a comment |
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%2f53451987%2fretrofit2-doesnt-consume-endpoint-spotify-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The code:
.map(token->token.toString());
does do something: it returns a modified observable. However, the modified observable is then thrown away. An observable has to be subscribed in order to activate it.
It's possible you can get away with adding the operator at the end:
.map(token->token.toString())
.subscriber();
However, it isn't clear what you want to do with the mapped string.
Yes, that worked. I didn't want to do anything with the string, I just wanted to show on console if the call was done and if the token model was mapped correctly. Thanks for your help
– David
Nov 23 '18 at 23:11
add a comment |
The code:
.map(token->token.toString());
does do something: it returns a modified observable. However, the modified observable is then thrown away. An observable has to be subscribed in order to activate it.
It's possible you can get away with adding the operator at the end:
.map(token->token.toString())
.subscriber();
However, it isn't clear what you want to do with the mapped string.
Yes, that worked. I didn't want to do anything with the string, I just wanted to show on console if the call was done and if the token model was mapped correctly. Thanks for your help
– David
Nov 23 '18 at 23:11
add a comment |
The code:
.map(token->token.toString());
does do something: it returns a modified observable. However, the modified observable is then thrown away. An observable has to be subscribed in order to activate it.
It's possible you can get away with adding the operator at the end:
.map(token->token.toString())
.subscriber();
However, it isn't clear what you want to do with the mapped string.
The code:
.map(token->token.toString());
does do something: it returns a modified observable. However, the modified observable is then thrown away. An observable has to be subscribed in order to activate it.
It's possible you can get away with adding the operator at the end:
.map(token->token.toString())
.subscriber();
However, it isn't clear what you want to do with the mapped string.
answered Nov 23 '18 at 21:49
Bob DalgleishBob Dalgleish
6,35222436
6,35222436
Yes, that worked. I didn't want to do anything with the string, I just wanted to show on console if the call was done and if the token model was mapped correctly. Thanks for your help
– David
Nov 23 '18 at 23:11
add a comment |
Yes, that worked. I didn't want to do anything with the string, I just wanted to show on console if the call was done and if the token model was mapped correctly. Thanks for your help
– David
Nov 23 '18 at 23:11
Yes, that worked. I didn't want to do anything with the string, I just wanted to show on console if the call was done and if the token model was mapped correctly. Thanks for your help
– David
Nov 23 '18 at 23:11
Yes, that worked. I didn't want to do anything with the string, I just wanted to show on console if the call was done and if the token model was mapped correctly. Thanks for your help
– David
Nov 23 '18 at 23:11
add a comment |
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%2f53451987%2fretrofit2-doesnt-consume-endpoint-spotify-api%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