使用Retrofit与Rails API

1 人关注

我正在开发一个Android应用程序。我决定使用Square的Retrofit库,它符合我的要求。然而,我面临的问题是 反序列化问题 .我的后台是通过使用 rails-api gem在Rails 4中编写的。 active-model-serializer 可以生成这种JSON(JSON有一个根元素)。

{"user":{"id":1,"name":"Saygun","github_url":"www.github.com/saygun" }}

以下是我如何初始化retrofit

public class User {
    private String mId;
    private String mName;
    private String mGithubUrl;
    public String getId() {
        return mName;
    public void setId(String id) {
        this.mId = id;
    public String getName() {
        return mName;
    public void setName(String name) {
        this.mName = name;
    public String getGithubUrl(String url) {
        this.mGithubUrl = url;
public interface RestService {
    @GET("/users/{id}")
    void getUser(@Path("id") int id, Callback<User> cb);

Gson gson = new GsonBuilder() .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .registerTypeAdapter(Date.class, new DateTypeAdapter()) .create();

RestAdapter restAdapter = new RestAdapter.Builder()
        .setEndpoint(BuildConfig.API_ENDPOINT)
        .setLogLevel(RestAdapter.LogLevel.FULL)
        .setRequestInterceptor(requestInterceptor)
        .setConverter(new GsonConverter(gson))
        .build();
RestService service = restAdapter.create(RestService.class);
service.getUser(1, new Callback<User>() {
     @Override
     public void success(User user, Response response) {
          //User is null
     @Override
     public void failure(RetrofitError error) {

有没有人可以告诉我,我错过了什么。为什么retrofit不能创建用户?

1 个评论
Did you get this resolved?
android
ruby-on-rails-4
gson
retrofit
active-model-serializers
saygun
saygun
发布于 2015-04-11
2 个回答
Soroush
Soroush
发布于 2021-12-03
0 人赞同

我认为你应该在你的字段上使用与API类相同的名称 并在你的客户端类中实现Serializable。

something like this :

public class User implements Serializable {
public String id ;
public String name;
public String github_url;

或者你也可以使用@SerializedName("serialized_name")。

like this :

public class User implements Serializable {
    @SerializedName()
    private String mId;
    @SerializedName()
    private String mName;
    @SerializedName()
    private String mGithubUrl;
    
yuan
yuan
发布于 2021-12-03
0 人赞同

This is my solution (kotlin version)

class UserModel(
    val id: Long,
    val name: String,
    val githubUrl: String
class UserResponse(
    @SerializedName("user") val model: UserModel
class PagedUserListResponse(
    val metaData: PageMetaData,
    @SerializedName("users") val items: List<UserModel>
) : PageMeta by metaData
interface PageMeta {
    val totalPages: Int
    val currentPage: Int
    val count: Int
    val previousPage: Int
    val nextPage: Int
class PageMetaData(
    override val totalPages: Int,
    override val count: Int,
    override val previousPage: Int,
    override val nextPage: Int,
    override val currentPage: Int
) : PageMeta
interface UserAPICaller {
    @GET("users")
    suspend fun index(@Query("page") page: Int?, @Query("per_page") perPage: Int?): PagedUserListResponse
    @POST("users")
    suspend fun create(@Body request: UserRequest): UserResponse