【发布时间】:2016-01-27 10:39:12
【问题描述】:
我的响应 json 像
[{"user":"hasan","name":"hasan hamza"},{"user":"hüseyin","name":"hüseyin tırlak"}]
和我的班级一样
public class Person {
public String user;
public String name;
}
还有我的改造服务界面,比如
public interface IService {
@GET(ServiceURLs.USERS)
Call<List<Person>> getUsers(@Path("operation") String operation);
}
我这样称呼这个接口
retrofit= new Retrofit.Builder()
.baseUrl(ServiceURLs.BASE_SERVICE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
IService service = retrofit.create(IService.class);
Call<List<Person>> response = service.getUsers(OperationEnum.SHOW.getOperation());
response.enqueue(new Callback<List<Person>>() {
@Override
public void onResponse(Response<List<Person>> response, Retrofit retrofit) {
if (response.isSuccess()) {
Log.d("Size", response.body().size()+"");
} else {
// error response, no access to resource?
}
}
@Override
public void onFailure(Throwable t) {
// something went completely south (like no internet connection)
Log.d("Error", t.getMessage());
}
});
当我提出这个请求时,它落在 onFailure 方法上,它说
java.lang.IllegalStateException:应为 BEGIN_ARRAY,但在第 1 行第 1 列路径 $ 改造处为 STRING
我该如何解决它
我的配置
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.xyz.damn"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.okhttp:okhttp:2.5.0'
}
【问题讨论】:
-
public interface IService { @GET(ServiceURLs.USERS) Call
- > getUsers(@Path("operation") String operation,new Callback
callback);您可以更新此代码并重试吗? } -
你的错误信息基本上是在告诉你你的反应不是你想象的那样。启用登录改造并重试。
-
mustafasevgi 我认为您的改装版本有点旧。我检查了我的代码,但仍然存在错误
-
njzk2 我知道它告诉它是什么和问题,因为它等待 { } 这种 json 启动但它以 [ ] 开头
-
将@GET("/users") 改为@GET("users") 也可以解决问题。我也有。