我正在使用Retrofit调用服务器并获取json响应。我得到的错误是:
Expected BEGIN_OBJECT but was BEGIN_ARRAY
这是我的代码
Main method,
public class MainActivity extends ListActivity {
List<Movie> movieList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RestAdapter restadapter = new RestAdapter.Builder().setEndpoint("http://api.themoviedb.org").build();
api movieapi = restadapter.create(api.class);
movieapi.getData(new Callback<List<Movie>>() {
@Override
public void success(List<Movie> movies, Response response) {
movieList = movies;
adapter adapt = new adapter(getApplicationContext(),R.layout.item_file,movieList);
//ListView listView = (ListView) findViewById(R.id.list);
setListAdapter(adapt);
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
接口api
public interface api {
@GET("/3/discover/movie?sort_by=popularity.desc&api_key=e2a804764747474747474574817b218e")
public void getData(Callback<List<Movie>> response);
}
类适配器
public class adapter extends ArrayAdapter<Movie> {
String url = "http://image.tmdb.org/t/p/w185";
private Context context;
private List<Movie> movieList;
public adapter(Context context, int resource, List<Movie> objects) {
super(context, resource, objects);
this.context = context;
this.movieList = 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.item_file,parent,false);
Movie movie = movieList.get(position);
TextView tv = (TextView) view.findViewById(R.id.name);
tv.setText(movie.getTitle());
ImageView img = (ImageView) view.findViewById(R.id.img);
Picasso.with(getContext()).load(url+movie.getPoster_path()).into(img);
return view;
}
}
电影课(我在哪里处理json的getter setters)
package com.wuno.moviesapp.model;
public class Movie
{
private String vote_average;
private String backdrop_path;
private String adult;
private String id;
private String title;
private String original_language;
private String overview;
private String[] genre_ids;
private String original_title;
private String release_date;
private String vote_count;
private String poster_path;
private String video;
private String popularity;
public String getVote_average ()
{
return vote_average;
}
public void setVote_average (String vote_average)
{
this.vote_average = vote_average;
}
public String getBackdrop_path ()
{
return backdrop_path;
}
public void setBackdrop_path (String backdrop_path)
{
this.backdrop_path = backdrop_path;
}
public String getAdult ()
{
return adult;
}
public void setAdult (String adult)
{
this.adult = adult;
}
public String getId ()
{
return id;
}
public void setId (String id)
{
this.id = id;
}
public String getTitle ()
{
return title;
}
public void setTitle (String title)
{
this.title = title;
}
public String getOriginal_language ()
{
return original_language;
}
public void setOriginal_language (String original_language)
{
this.original_language = original_language;
}
public String getOverview ()
{
return overview;
}
public void setOverview (String overview)
{
this.overview = overview;
}
public String[] getGenre_ids ()
{
return genre_ids;
}
public void setGenre_ids (String[] genre_ids)
{
this.genre_ids = genre_ids;
}
public String getOriginal_title ()
{
return original_title;
}
public void setOriginal_title (String original_title)
{
this.original_title = original_title;
}
public String getRelease_date ()
{
return release_date;
}
public void setRelease_date (String release_date)
{
this.release_date = release_date;
}
public String getVote_count ()
{
return vote_count;
}
public void setVote_count (String vote_count)
{
this.vote_count = vote_count;
}
public String getPoster_path ()
{
return poster_path;
}
public void setPoster_path (String poster_path)
{
this.poster_path = poster_path;
}
public String getVideo ()
{
return video;
}
public void setVideo (String video)
{
this.video = video;
}
public String getPopularity ()
{
return popularity;
}
public void setPopularity (String popularity)
{
this.popularity = popularity;
}
@Override
public String toString()
{
return "Movie [vote_average = "+vote_average+", backdrop_path = "+backdrop_path+", adult = "+adult+", id = "+id+", title = "+title+", original_language = "+original_language+", overview = "+overview+", genre_ids = "+genre_ids+", original_title = "+original_title+", release_date = "+release_date+", vote_count = "+vote_count+", poster_path = "+poster_path+", video = "+video+", popularity = "+popularity+"]";
}
}
答案 0 :(得分:0)
Retrofit尝试将结果解析为您指定的类型,而这不是返回的类型。您指定的列表是返回的对象的成员。
您应该为响应创建一个对象:
public class MoviesResponse {
private int page;
private List<Movie> results;
private int total_pages;
private int total_results;
}
更改回调:
@GET("/3/discover/movie?sort_by=popularity.desc&api_key=your_api_key")
public void getData(Callback<MoviesResponse> response);