使用Android中的Retrofit获取带参数的数据

时间:2017-05-12 11:16:22

标签: android get retrofit

我尝试使用Laravel REST API和Retrovit从数据库中获取数据。我使用一个参数来获取我想要的数据,但它会一直返回onFailure并且我不知道为什么。这是我的代码:

API接口

@GET("getDetilFilm/{idFilm}/getDetilFilm")
Call<Film> getDetilFilm(@Query("idFilm") String idFilm);

的活动:

public class DetilFilmActivity extends AppCompatActivity {

    ApiInterface mApiInterface;

    @InjectView(R.id.tvJudulFilm)
    TextView judulFilm;
    @InjectView(R.id.tvDurasi)
    TextView durasi;
    @InjectView(R.id.tvGenre)
    TextView genre;
    @InjectView(R.id.tvSinopsis)
    TextView sinopsis;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detil_film);
        ButterKnife.inject(this);
        getDetilFilmById();
    }

    private void getDetilFilmById(){

        mApiInterface = ApiClient.getClient().create(ApiInterface.class);

        Call<Film> callDetilFilm= mApiInterface.getDetilFilm("6");
        callDetilFilm.enqueue(new Callback<Film>() {
            @Override
            public void onResponse(Call<Film> call, Response<Film> response) {
                Film detil =response.body();
                genre.setText(detil.getIdGenre());
                judulFilm.setText(detil.getJudulFilm());
                sinopsis.setText(detil.getKeterangan());
                durasi.setText(detil.getDurasi());
            }

            @Override
            public void onFailure(Call<Film> call, Throwable t) {
                Toast.makeText(DetilFilmActivity.this, "Failed to load", Toast.LENGTH_LONG).show();
            }
        });
    }
}

电影课

    public class Film {

    @SerializedName("judulFilm")
    private String judulFilm;
    @SerializedName("durasi")
    private String durasi;
    @SerializedName("keterangan")
    private String keterangan;
    @SerializedName("idGenre")
    private String idGenre;
    @SerializedName("idFilm")
    private String idFilm;
    private String foto;
    private String flag;
    private String created_at;
    private String updated_at;
    @SerializedName("backdrop_path")
    private String backdropPath;

    public Film(String judulFilm, String durasi, String keterangan, String idGenre, String idFilm, String foto, String flag, String created_at, String updated_at) {
        this.judulFilm = judulFilm;
        this.durasi = durasi;
        this.keterangan = keterangan;
        this.idGenre = idGenre;
        this.idFilm = idFilm;
        this.foto = foto;
        this.flag = flag;
        this.created_at = created_at;
        this.updated_at = updated_at;
    }

    public String getJudulFilm() {
        return judulFilm;
    }

    public void setJudulFilm(String judulFilm) {
        this.judulFilm = judulFilm;
    }

    public String getDurasi() {
        return durasi;
    }

    public void setDurasi(String durasi) {
        this.durasi = durasi;
    }

    public String getKeterangan() {
        return keterangan;
    }

    public void setKeterangan(String keterangan) {
        this.keterangan = keterangan;
    }

    public String getIdGenre() {
        return idGenre;
    }

    public void setIdGenre(String idGenre) {
        this.idGenre = idGenre;
    }

    public String getIdFilm() {
        return idFilm;
    }

    public void setIdFilm(String idFilm) {
        this.idFilm = idFilm;
    }

    public String getFoto() {
        return foto;
    }

    public void setFoto(String foto) {
        this.foto = foto;
    }

    public String getBackdropPath() {
        return backdropPath;
    }

    public void setBackdropPath(String backdropPath) {
        this.backdropPath = backdropPath;
    }
}

修改

我将onResponse更改为此

@Override
        public void onResponse(Call<List<Film>> call, Response<List<Film>> response) {
            List<Film> detil =response.body();
            try
            {
                for(int i=0;i<=detil.size();i++)
                {
                        genre.setText(detil.get(i).getIdGenre());
                        judulFilm.setText(detil.get(i).getJudulFilm());
                        sinopsis.setText(detil.get(i).getKeterangan());
                        durasi.setText(detil.get(i).getDurasi());

                }
            }
            catch(Exception e)
            {
                Log.e("onFailure: ", e.getMessage(), e);
                Log.d("onFail", call.request().url().toString());
            }
        }

错误更改为

 java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

有人可以帮忙吗?提前致谢

0 个答案:

没有答案