接受错误的json
我是第一次使用改造。我想从API返回身份验证,但未调用OnResponse。这是我的代码:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://abc.def.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiInterface service = retrofit.create(ApiInterface.class);
String setAPI_KEY = "123456";
String settoken = "7891011";
retrofit2.Call<TokenResponse> tokenResponseCall = service.getToken(setAPI_KEY, settoken);
tokenResponseCall.enqueue(new Callback<TokenResponse>() {
@Override
public void onResponse(retrofit2.Call<TokenResponse> call, retrofit2.Response<TokenResponse> response) {
int a = response.code();
TokenResponse tokenResponse = new TokenResponse();
}
@Override
public void onFailure(retrofit2.Call<TokenResponse> call, Throwable t) {
System.out.print("Fail");
}
});
在接口中,我将其设置为这样:
public interface ApiInterface {
@GET("/qwe/asd/zxc")
Call<TokenResponse> getToken(@Query("key") String API_KEY,@Query("acesstoken") String token);
}
有什么想法怎么了?完整的URL就像这样
https://abc.def.com/qwe/asd/zxc?key=key&acestoken=token
这是我的tokenresponse类:
public class TokenResponse {
@SerializedName("authorization_key")
private String authorization_key;
public String getAuthorization_key() {
return authorization_key;
}
}
我得到的例外是:com.google.gson.stream.malformedjsonexception:使用jsonreader.setLenient(true(在第1列第1列路径$
gson在对象结束后没有空间的额外字符时会引发该特定错误,并且它非常狭窄地定义了空格。
使用它
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://abc.def.com/")
.addConverterFactory(GsonConverterFactory.create(gson))
.build();