question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I use moshi-jsonapi in Retrofit2?

See original GitHub issue

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

    }
});

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
kamikatcommented, Nov 16, 2016

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.

0reactions
kamikatcommented, Apr 27, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I use jsonapi in Retrofit2? - Stack Overflow
From moshi-jsonapi documentation you need to add the libraries factory to the moshi instance: // First create the factory JsonAdapter.
Read more >
Moshi with Retrofit in Kotlin - ProAndroidDev
Hello everyone! Today I want to show how to implement Moshi with Retrofit (new project or replacing Gson) in an Android project.
Read more >
How to parse JSON with Retrofit Converters using Kotlin
In this tutorial, I will show you how to parse JSON using the Retrofit converters: Gson, Moshi, Kotlinx Serialization and Jackson!
Read more >
How to Send JSON Data in a POST Request in Android
I have put together a tutorial with code samples in Java covering how to send JSON data in a HTTP POST request within...
Read more >
Everything You Need To Know About Retrofit and Moshi in ...
In this video, you will learn about Retrofit in Android To become a great Android Developer check out our Android Learning Path: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found