改造:预期begin_array但是Begin_OBJECTS

时间:2015-05-28 03:22:17

标签: android api retrofit

首先我有这个Json Objects From Server,我去了显示曲目名称是List Using Retrofit Lib。但它的回归发生了预期的BEGIN_ARRAY但是在COLOMUNS 1开始了...

{ "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;
} ... 

这个Tracks课程:

public class Tracks {

private Toptracks toptracks;

public Toptracks getToptracks() {
    return toptracks;
} ... 

跟踪课程:getter和setter ..

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);

列表中显示名称的Adapter类。

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;
}

最后在MainActivity中我称之为:

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 个答案:

答案 0 :(得分:0)

更改此行

@GET("/?method=geo.getmetrotrackchart...")
void getNews(Callback<Toptracks> response);

TopTracks

由于您的响应以JSONObject开头,因此您需要使用public class YourResponse{ private Toptracks toptracks; //create getter and setter.... } 类。在响应CallBack中做同样的更改。

<强>已更新

YourResponse.class

void getNews(Callback<YourResponse> response);

设置回复

  response.getToptracks().getTrack(); //will get list of tracks

获取跟踪onSuccess()方法响应对象

<TextBox Text="Data" Width="200" Height="50" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" TextAlignment="Center"/>