首先,我从服务器获得了这个Json对象,然后使用reverfit lib显示曲目名称是列表。但其返回此问题应为BEGIN_ARRAY,但在COLOMUNS 1处为BEGIN_OBJECTS。
{ "toptracks" : { "@attr" : { "metro" : "Seattle",
"page" : "1",
"perPage" : "50",
"total" : "500",
"totalPages" : "10"
},
"track" : [ { "@attr" : { "rank" : "1" },
"artist" : { "mbid" : "381086ea-f511-4aba-bdf9-71c753dc5077",
"name" : "Kendrick Lamar",
"url" : "http://www.last.fm/music/Kendrick+Lamar"
},
"duration" : "195",
"image" : [ { "#text" : "http://userserve-ak.last.fm/serve/34s/103947943.png",
"size" : "small"
},
{ "#text" : "http://userserve-ak.last.fm/serve/64s/103947943.png",
"size" : "medium"
},
{ "#text" : "http://userserve-ak.last.fm/serve/126/103947943.png",
"size" : "large"
},
{ "#text" : "http://userserve-ak.last.fm/serve/300x300/103947943.png",
"size" : "extralarge"
}
],
"listeners" : "122",
"mbid" : "2d584d3c-d6ce-40ec-b23a-7b29c687ac07",
"name" : "King Kunta",
"streamable" : { "#text" : "0",
"fulltrack" : "0"
},
"url" : "http://www.last.fm/music/Kendrick+Lamar/_/King+Kunta"
},
{ "@attr" : { "rank" : "2" },
"artist" : { "mbid" : "d8915e13-d67a-4aa0-9c0b-1f126af951af",
"name" : "Hot Chip",
"url" : "http://www.last.fm/music/Hot+Chip"
},
"duration" : "329",
"image" : [ { "#text" : "http://userserve-ak.last.fm/serve/34s/103606721.gif",
"size" : "small"
},
{ "#text" : "http://userserve-ak.last.fm/serve/64s/103606721.gif",
"size" : "medium"
},
{ "#text" : "http://userserve-ak.last.fm/serve/126/103606721.gif",
"size" : "large"
},
{ "#text" : "http://userserve-ak.last.fm/serve/300x300/103606721.gif",
"size" : "extralarge"
}
], ...
我正在使用这些类:
public class Toptracks {
private List<Track> track = new ArrayList<Track>();
private Attr_ Attr;
public List<Track> getTrack() {
return track;
} ...
并且此跟踪类:
public class Tracks {
private Toptracks toptracks;
public Toptracks getToptracks() {
return toptracks;
} ...
public class Track {
private String name;
private String duration;
private String listeners;
private String mbid;
private String url;
private Streamable streamable;
private Artist artist;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} ...
@GET("/?method=geo.getmetrotrackchart...")
void getNews(Callback<List<Track>> responce);
public class TracksAdapter extends ArrayAdapter<Track> {
private Context context;
private List<Track> trackList;
public TracksAdapter(Context context, int resource, List<Track> objects) {
super(context, resource, objects);
this.context = context;
this.trackList = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.news_shape, parent, false);
//Dispaly News..
Track track = trackList.get(position);
TextView newsTitle = (TextView) view.findViewById(R.id.news_title_tv);
TextView newsDate = (TextView) view.findViewById(R.id.news_date_tv);
TextView newsBody = (TextView) view.findViewById(R.id.news_body);
newsTitle.setText(track.getName());
newsDate.setText(track.getArtist().getName());
return view;
}
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(AppConfig.LAST_FM)
.build();
RestAPI api = adapter.create(RestAPI.class);
api.getNews(new Callback<List<Track>>() {
@Override
public void success(List<Track> toptracks, Response response) {
tracksList = toptracks;
TrackAdapter tracksAdapter = new TracksAdapter(MainActivity.this, R.layout.news_shape, tracksList);
setListAdapter(tracksAdapter);
}
更改此行
@GET("/?method=geo.getmetrotrackchart...")
void getNews(Callback<List<Track>> response);
至
@GET("/?method=geo.getmetrotrackchart...")
void getNews(Callback<Toptracks> response);
由于您的响应是从JSONObject开始,所以您需要使用toptracks
类。在响应回调中进行相同的更改。
public class YourResponse{
private Toptracks toptracks;
//create getter and setter....
}
void getNews(Callback<YourResponse> response);
response.getToptracks().getTrack(); //will get list of tracks
有改装API 附注。很抱歉可能的语法错误。
拜托,我有麻烦了。我想获取web api()上的所有实体
我正在尝试使用改版在我的应用程序上实现用户注册,但是我一直得到这个错误不确定是什么错误,java.lang.IllegalStateException:应该是BEGIN_OBJECT,但应该是BEGIN_ARRAY 这是邮递员的回复 } 我有两个模型类User模型类 和我的register类 如果可能的话有人帮我实现Kotlin coroutines
这是我的界面: 这是我希望将收到的数据转换到的县数据类: ...Getters和setters...