kamikat / moshi-jsonapi

JSON API v1.0 Specification in Moshi.
MIT License
156 stars 34 forks source link

How can I use moshi-jsonapi in Retrofit2? #30

Closed mirmousaviii closed 7 years ago

mirmousaviii commented 7 years ago

TokenModel.java

@JsonApi(type = "tokens")
public class TokenModel extends Resource {
    @Json(name = "cell_phone")
    public String cellPhone;
}

TestService.java:

public interface TestService {
    @POST("token")
    Call<TokenModel> newOtp(@Body TokenModel tokenModel);
}

TestProvider.java:

public class TestProvider {
    private TestService testService;

    public TestProvider() {
        OkHttpClient httpClient = new OkHttpClient();
        Retrofit refRetrofit = new Retrofit.Builder()
                .baseUrl(ClientConfigs.BASE_URL)
                .client(httpClient)
                .addConverterFactory(MoshiConverterFactory.create())
//                .addConverterFactory(????????????????????????????)
                .build();
        testService = refRetrofit.create(TestService.class);
    }

    publicTestService getTestService() {
        return testService;
    }
}

If I use MoshiConverterFactory make error Unable to create converter for class com.xxxx.xxxx.model.TokenModel!

Use Retrofit:

TsetProvider testProvider = new TestProvider();
TestService testService = testProvider.getTestService();

TokenModel tokenModel = new TokenModel();
tokenModel.cellPhone = "123123123";

Call<TokenModel> call = testService.newOtp(tokenModel);
call.enqueue(new Callback<TokenModel>() {
    @Override
    public void onResponse(Call<TokenModel> call, Response<TokenModel> response) {
    }

    @Override
    public void onFailure(Call<TokenModel> call, Throwable t) {

    }
});
kamikat commented 7 years ago

Here is a boilerplate code for a moshi converter factory:

public static Converter.Factory createMoshiConverterFactory() {
    Moshi moshi = new Moshi.Builder()
            .add(ResourceAdapterFactory.builder()
                    .add(Credential.class)
                    .add(Event.class)
                    .add(Notification.class)
                    .add(Order.class)
                    .add(Product.class)
                    .add(Session.class)
                    .add(User.class)
                    .add(AnyModelClassYouNeed.class)
                    .build())
            .add(Date.class, new Rfc3339DateJsonAdapter())
            .build();
    return MoshiConverterFactory.create(moshi);
}

I'd add that to documentation ASAIC.

mirmousaviii commented 7 years ago

Thank you. It's resolved with your code. And I find other solution same your answer.

senthilkumar06 commented 7 years ago

ResourceAdapterFactory is not resolved.

kamikat commented 7 years ago

@senthilkumar06 can you check if the library is correctly added to the project?

lkakireddy commented 6 years ago

Cannot resolve symbol 'MoshiConverterFactory'

kamikat commented 6 years ago

@lkakireddy Here is the gist for retrofit integration but it seems to be broken for now 🤕