我正在使用Retrofit2
,我想发布我们的服务器以获取一些json数据。所以我有2个班级。这是我的回复课:
public class RegisterToken {
private String accessToken;
private String tokenType;
private Integer expiresIn;
private String refreshToken;
private String asClientId;
private String Issued;
private String Expires;
private Map<String, Object> additionalProperties = new HashMap<>();
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
//and the other setter and getter,actually dont write cuz its too long
}
然后这是我的API帖子界面:
public interface APIService {
@FormUrlEncoded
@POST("token")
public Callback<RegisterToken> getRegisterToken(@Header("Accept")String accept,
@Header("Content-Type")String contentType,@Header("register") String registerActive,
@Field("grant_type") String grantType,@Field("client_id") String clientId,
@Field("client_secret") String clientSecret,Callback<RegisterToken> callback);
}
在活动中我这样做:
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
Retrofit client = new Retrofit.Builder()
.baseUrl(getResources().getString(R.string.base_api_url))
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
APIService service = client.create(APIService.class);
Callback<RegisterToken> registerToken=service.getRegisterToken("application/json",
"application/json", "active", grant_type, client_id, client_secret,
new Callback<RegisterToken>() {
@Override
public void onResponse(retrofit2.Response<RegisterToken> response) {
Log.v("good","okkay");
}
@Override
public void onFailure(Throwable t) {
Log.v("bad","shithapp");
}
});
所以我收到了这个错误:
java.lang.IllegalArgumentException: Unable to create call adapter for
retrofit2.Callback<RegisterToken> for method APIService.getRegisterToken
这里有什么问题我找不到,我曾经尝试过并查看了很多Stackoverflow问题并且无法找到解决方案:(
答案 0 :(得分:1)
在界面APIService
中,将getRegisterToken(...)
的返回类型更改为void
。您将通过最后一个回调参数通知您POST结果。
因此,请在Callback<RegisterToken> registerToken =
之前删除service.getRegisterToken
。