繁体   English   中英

无法使用Retrofit 2发布JSON

[英]Can't post JSON with Retrofit 2

当试图POST使用改造一些JSON数据2.0.0-beta4 ,我发现了以下错误:

java.lang.IllegalArgumentException: Unable to create @Body converter for class my.pojos.Credentials

...

Caused by: java.lang.IllegalArgumentException: Could not locate RequestBody converter for class my.pojos.Credentials.
Tried:
* retrofit2.BuiltInConverters
* retrofit2.GsonConverterFactory
  at retrofit2.Retrofit.nextRequestBodyConverter(Retrofit.java:288)
...

不知道这是怎么回事。 据我所知,我的设置是逐字逐字地遵循其他可能工作的示例。

我的应用程序级别build.gradle

dependencies {
  ...
  compile 'com.google.code.gson:gson:2.5'
  compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
  compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
  compile 'com.squareup.okhttp:okhttp:2.7.0'
  ...
}

而我的改造建造者:

Gson gson = new GsonBuilder()
        .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
        .create();

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(context.getString(R.string.rest_url))
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();

有什么想法我做错了吗?

编辑

我的API界面如下所示:

import retrofit2.Call;
import retrofit2.http.*;

public interface MyRestApi {
    @POST("/auth")
    Call<Auth> login(@Body Credentials user);
}

和API调用:

Call<Auth> authCall = retrofit.create(MyRestApi.class).login(creds);

authCall.enqueue(new Callback<Auth>() {
     @Override
     public void onResponse(Call<Auth> call, Response<Auth> response) {
         ...
     }

     @Override
     public void onFailure(Call<Auth> call, Throwable t) {
         ...
     }
});

看起来像我的问题是,我有beta3在我的GSON转换器build.gradle ,但beta4改造的。 将我的build.gradle更改为以下内容可以正常工作:

dependencies {
    compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
    compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
}

截至今日

这些是依赖项

compile 'com.squareup.retrofit2:converter-gson:2.0.1'

请在此处检查最新版本http://search.maven.org/#artifactdetails%7Ccom.squareup.retrofit2%7Cconverter-gson%7C2.0.1%7Cjar

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM