改造 + GSON = 进入 "onFailure"
Retrofit + GSON = entering "onFailure"
我正在尝试首先使用 Retrofit 为课程获取数据。但是我发现我的程序会转到 "onFailure" 选项。
错误信息:
2020-03-07 18:54:04.499 17756-17756/com.example.apirequest I/MainActivity: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
基本上,我首先尝试从 API https://api.spacexdata.com/v3/launches/
中获取 "launch_year"
谁能帮我找出我做不对的原因?
我在年级添加了:
implementation 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
我的代码如下:
主要活动:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Retrofit retrofit = new Retrofit.Builder().
baseUrl(APIService.urlBase)
.addConverterFactory(GsonConverterFactory.create()).
build();
APIService service = retrofit.create(APIService.class);
Call<rocketCatalog> requestModels = service.listCaralog();
requestModels.enqueue(new Callback<rocketCatalog>() {
@Override
public void onResponse(Call<rocketCatalog> call, Response<rocketCatalog> response) {
rocketCatalog catalog = response.body();
for (Rockets rocket : catalog.rocket){
Log.i(TAG,String.format("%s",rocket.getLaunch_date()));
}
}
@Override
public void onFailure(Call<rocketCatalog> call, Throwable t) {
Log.i(TAG,String.format("============= Failure =============="));
}
});
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
}
}
API服务:
public interface APIService {
public static final String urlBase = "https://api.spacexdata.com/v3/";
@GET("launches")
Call<rocketCatalog> listCaralog();
}
火箭目录:
public class rocketCatalog {
public List<Rockets> rocket;
public List<Rockets> getRocket() {
return rocket;
}
public void setRocket(List<Rockets> rocket) {
this.rocket = rocket;
}
}
火箭队:
public class Rockets {
String launch_date;
public String getLaunch_date() {
return launch_date;
}
public void setLaunch_date(String launch_date) {
this.launch_date = launch_date;
}
}
您的端点 returns 是 List
,因此您的响应需要 List
类型。
public static final String urlBase = "https://api.spacexdata.com/v3/";
@GET("launches")
Call<List<Rockets>> listCaralog();
还要确保 Rockets
Class 中的字段 launch_date
存在于响应中。
在你的帮助下,我明白了发生了什么。
确实我收到了一份清单,但我不得不重新构建,我已经按照 Mohamed Mohsin 的建议进行修改并将我的 rocketCatalog.java 修改为:
火箭目录:
String launch_year;
public String getLaunch_year() {
return launch_year;
}
public void setLaunch_year(String launch_date) {
this.launch_year = launch_date;
}
}
另外,我把错误的"launch_date"改成了"launch_year"
再次感谢您!
我正在尝试首先使用 Retrofit 为课程获取数据。但是我发现我的程序会转到 "onFailure" 选项。
错误信息:
2020-03-07 18:54:04.499 17756-17756/com.example.apirequest I/MainActivity: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
基本上,我首先尝试从 API https://api.spacexdata.com/v3/launches/
中获取 "launch_year"谁能帮我找出我做不对的原因?
我在年级添加了:
implementation 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
我的代码如下:
主要活动:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Retrofit retrofit = new Retrofit.Builder().
baseUrl(APIService.urlBase)
.addConverterFactory(GsonConverterFactory.create()).
build();
APIService service = retrofit.create(APIService.class);
Call<rocketCatalog> requestModels = service.listCaralog();
requestModels.enqueue(new Callback<rocketCatalog>() {
@Override
public void onResponse(Call<rocketCatalog> call, Response<rocketCatalog> response) {
rocketCatalog catalog = response.body();
for (Rockets rocket : catalog.rocket){
Log.i(TAG,String.format("%s",rocket.getLaunch_date()));
}
}
@Override
public void onFailure(Call<rocketCatalog> call, Throwable t) {
Log.i(TAG,String.format("============= Failure =============="));
}
});
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
}
}
API服务:
public interface APIService {
public static final String urlBase = "https://api.spacexdata.com/v3/";
@GET("launches")
Call<rocketCatalog> listCaralog();
}
火箭目录:
public class rocketCatalog {
public List<Rockets> rocket;
public List<Rockets> getRocket() {
return rocket;
}
public void setRocket(List<Rockets> rocket) {
this.rocket = rocket;
}
}
火箭队:
public class Rockets {
String launch_date;
public String getLaunch_date() {
return launch_date;
}
public void setLaunch_date(String launch_date) {
this.launch_date = launch_date;
}
}
您的端点 returns 是 List
,因此您的响应需要 List
类型。
public static final String urlBase = "https://api.spacexdata.com/v3/";
@GET("launches")
Call<List<Rockets>> listCaralog();
还要确保 Rockets
Class 中的字段 launch_date
存在于响应中。
在你的帮助下,我明白了发生了什么。
确实我收到了一份清单,但我不得不重新构建,我已经按照 Mohamed Mohsin 的建议进行修改并将我的 rocketCatalog.java 修改为:
火箭目录:
String launch_year;
public String getLaunch_year() {
return launch_year;
}
public void setLaunch_year(String launch_date) {
this.launch_year = launch_date;
}
}
另外,我把错误的"launch_date"改成了"launch_year"
再次感谢您!