2

retrofit2 でjsonapiを使用する必要があります。moshi-jsonapiを試してみましたが、moshi ConverterFactory を使用できません。

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

MoshiConverterFactorymake errorを使用するとUnable to create converter for class com.xxx.xxx.model.TokenModel!

レトロフィットを使用:

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

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

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) {
    }
});
4

1 に答える 1