Java 如何通过改装获取独特的数据
我试图从改造API中提取一些数据,其JSON如下所示:Java 如何通过改装获取独特的数据,java,android,retrofit,Java,Android,Retrofit,我试图从改造API中提取一些数据,其JSON如下所示: { "data":{ "id":"1", "type": null, "links":{ "self":null }, "attributes":{ "createdAt":null, "updatedAt":null, "slug":"teste", "synopsis
{
"data":{
"id":"1",
"type": null,
"links":{
"self":null
},
"attributes":{
"createdAt":null,
"updatedAt":null,
"slug":"teste",
"synopsis":"text",
然后,我尝试获得如下具体数据:
{
"data":{
"id":"1",
"type": null,
"links":{
"self":null
},
"attributes":{
"createdAt":null,
"updatedAt":null,
"slug":"teste",
"synopsis":"text",
型号:
public class Data {
private ArrayList<Movie> data;
public ArrayList<Movie> getData() {
return data;
}
public void setData(ArrayList<Animes> data) {
this.data = data;
}
}
ApiService:
@GET("movie/{id}")
Call<Data> obtainProfile(@Path("id") int id);
int id = getIntent().getIntExtra("ID", 0);
private void obtainData(final int id) {
ApiService service = retrofit.create(ApiService.class);
Call<Data> profileCall = service.obtainProfile(id);
profileCall.enqueue(new Callback<Data>() {
@Override
public void onResponse(Call<Data> call, Response<Data> response) {
if (response.isSuccessful()) {
Data data = response.body();
Movie Movie = data.getData().get(0);
Attributes attributes = movie.getAttributes();
textName.setText(attributes.getCanonicalTitle());
} else {
Log.e("INFO", "onResponse" + response.errorBody());
Toast.makeText(PerfilActivity.this, "Error connecting to server.", Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<Data> call, Throwable t) {
Log.e("INFO", "onErro" + t.getMessage());
}
});
}
@GET(“movie/{id}”)
调用获取配置文件(@Path(“id”)int-id);
int id=getIntent().getIntExtra(“id”,0);
私有void获取数据(最终整数id){
ApiService=reformation.create(ApiService.class);
Call Profile Call=服务。获取配置文件(id);
profileCall.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
数据=response.body();
Movie Movie=data.getData().get(0);
Attributes=movie.getAttributes();
textName.setText(attributes.getCanonicalTitle());
}否则{
Log.e(“INFO”,“onResponse”+response.errorBody());
Toast.makeText(perfiActivity.this,“连接到服务器时出错”,Toast.LENGTH_LONG.show();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(“INFO”,“onErro”+t.getMessage());
}
});
}
我怎样才能解决这个问题?我试过了,但无法查看数据。
我需要为此创建一个列表吗?
我怎样才能解决这个问题?我试过了,但无法查看数据。
我需要为此创建一个列表吗?将您的
ArrayList
更改为仅Movie
还要创建另一个类:
public class DataResponse {
public Data data;
}
并将您的改装方法更改为:
@GET("movie/{id}")
Call<DataResponse> obtainProfile(@Path("id") int id);
@GET(“movie/{id}”)
调用获取配置文件(@Path(“id”)int-id);
使用json并将其转换为pojo。您的json数据中没有jsonarray您好!这个DataBufferResponse在我看来不是吗,只是DataBufferResponse,是吗?我的意思是创建另一个名为DataResponse的类。来自服务器的JSON与您为改型提供的JSON不匹配。你必须创建与你的JSON完全匹配的类和字段。你能给我一个例子吗?我是改装新手。你确定你的JSON是来自服务器的实际JSON吗?因为您的JSON没有movies.have的列表。我杀了他,有更多的数据。