2015-11-07 93 views
0

我正在使用Retrofit调用服务器并获得json响应。我得到的错误是:Retrofit - 预计BEGIN_OBJECT,但是BEGIN_ARRAY错误

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的getters 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

@乌迪丹:你不应该把这种谈话带到聊天室吗?你认为这真的是一个很好的问题,20条评论? –

+0

我回答了,虽然我现在还没有安卓工作室,所以可能会出现错误。祝你好运 –

回答

0

改装会尝试将结果解析为您指定的类型,但这不是返回的类型。您指定的列表是返回对象的成员。

你应该为响应创建一个对象:

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

这将取代我目前的电影课的权利? – wuno

+0

不,列表需要电影类

+0

非常感谢您的帮助,我了解您说的基本知识,但是我将无法贯穿始终。你知道如何在网上我可以去阅读,以解决这个问题与你的答案?例如,当你在界面中的列表中改变类型时,它被称为主列表中的列表有错误,所以列表只需要被删除,然后需要在你建议我理解的新类中创建getter和setter。 。 – wuno

相关问题