当前位置: 首页 > 知识库问答 >
问题:

改型:应为begin_array,但为Begin_OBJECTS

顾高翰
2023-03-14

首先,我从服务器获得了这个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);

        }

共有1个答案

刘泰
2023-03-14

更改此行

@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
 类似资料: