【问题标题】:Retrofit2 doesn't consume endpoint - Spotify APIRetrofit2 不消耗端点 - Spotify API
【发布时间】:2018-11-23 19:21:49
【问题描述】:

我正在尝试将 Retrofit2 与 Observables 和 Lombok 一起用于我的模型,以获取 spotify API 的令牌。我尝试使用邮递员,它工作正常,但我在使用 Java 时遇到问题。我有这 3 个类:

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);

}

我的模型,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 + '\'' +
            '}';
    }
}

这一行不显示任何内容:

.map(token-> token.toString());

这就像电话从未完成。不确定如何确认通话已完成,并且我尝试删除这 2 条 Observables 行

.subscribeOn(Schedulers.io()) .observeOn(Schedulers.computation())

然后返回

Single<Token> getToken(@Field("client_id") String clientId,

但没用。任何想法?提前致谢

【问题讨论】:

    标签: retrofit rx-java


    【解决方案1】:

    代码:

    .map(token->token.toString());
    

    做了一些事情:它返回一个修改后的 observable。然而,修改后的 observable 然后被丢弃。必须订阅 observable 才能激活它。

    你可以在最后添加操作符:

    .map(token->token.toString())
    .subscriber();
    

    但是,不清楚您想对映射的字符串做什么。

    【讨论】:

    • 是的,这行得通。我不想对字符串做任何事情,我只想在控制台上显示调用是否完成以及令牌模型是否正确映射。感谢您的帮助
    猜你喜欢
    • 2014-07-18
    • 2019-11-30
    • 1970-01-01
    • 2018-12-10
    • 2018-07-10
    • 1970-01-01
    • 2012-06-05
    • 2019-08-22
    • 1970-01-01
    相关资源
    最近更新 更多