Android Can';t post JSON与改装2
当尝试使用改型Android Can';t post JSON与改装2,android,json,retrofit2,Android,Json,Retrofit2,当尝试使用改型2.0.0-beta4发布一些JSON数据时,我得到以下错误: 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.
2.0.0-beta4
发布一些JSON数据时,我得到以下错误:
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);
}
2.调用;
导入文件2.http.*;
公共接口MyRestApi{
@POST(“/auth”)
调用登录(@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) {
...
}
});
callauthcall=reformation.create(MyRestApi.class).login(creds);
authCall.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
...
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
...
}
});
看起来我的问题是我在build.gradle中安装了gson转换器的beta3
,但是改装的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'
请在此处查看最新版本