[英]Can't post JSON with Retrofit 2
When trying to POST
some JSON data using Retrofit 2.0.0-beta4
, I'm getting the following error: 当试图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)
...
Not sure what's going on here. 不知道这是怎么回事。 As far as I know, my setup is following other presumably working examples verbatim. 据我所知,我的设置是逐字逐字地遵循其他可能工作的示例。
My app-level build.gradle
: 我的应用程序级别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'
...
}
And my retrofit builder: 而我的改造建造者:
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();
Any ideas what I'm doing wrong? 有什么想法我做错了吗?
Edit 编辑
My API interface looks like this: 我的API界面如下所示:
import retrofit2.Call;
import retrofit2.http.*;
public interface MyRestApi {
@POST("/auth")
Call<Auth> login(@Body Credentials user);
}
And the API call: 和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) {
...
}
});
Looks like my problem was that I had beta3
of the gson converter in my build.gradle
, but beta4
of retrofit. 看起来像我的问题是,我有beta3
在我的GSON转换器build.gradle
,但beta4
改造的。 Changing my build.gradle
to the following made things work: 将我的build.gradle
更改为以下内容可以正常工作:
dependencies {
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
}
As of today 截至今日
These are the dependencies 这些是依赖项
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
Do check here for latest version http://search.maven.org/#artifactdetails%7Ccom.squareup.retrofit2%7Cconverter-gson%7C2.0.1%7Cjar 请在此处检查最新版本http://search.maven.org/#artifactdetails%7Ccom.squareup.retrofit2%7Cconverter-gson%7C2.0.1%7Cjar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.