I'm trying to show some list in android using retrofit.
I'm using rest api laravel with url http://ecinema.esy.es/public/kota
the result
[
{
"idKota": "1",
"namaKota": "Yogyakarta",
"flag": "1",
"created_at": "2017-03-12 12:13:43",
"updated_at": "2017-03-12 12:13:43"
},
{
"idKota": "2",
"namaKota": "Jakarta",
"flag": "1",
"created_at": "2017-03-15 11:55:04",
"updated_at": "2017-03-14 07:17:54"
},
{
"idKota": "5",
"namaKota": "Semarang",
"flag": "1",
"created_at": "2017-03-22 08:11:19",
"updated_at": "2017-03-22 08:11:19"
}
]
ApiClient
public static final String BASE_URL = "http://ecinema.esy.es/public/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
GetKota
@SerializedName("status")
String status;
@SerializedName("result")
List<Kota> listDataKota;
@SerializedName("message")
String message;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<Kota> getListDataKota() {
return listDataKota;
}
public void setListDataKontak(List<Kota> listDataKota) {
this.listDataKota = listDataKota;
}
ApiInterface
@GET("kota")
Call<GetKota> getKota();
Adapter
List<Kota> mKotaList;
public KotaAdapter(List <Kota> KotaList) {
mKotaList= KotaList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.kota_list, parent, false);
MyViewHolder mViewHolder = new MyViewHolder(mView);
return mViewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.mTextViewNamaKota.setText(mKotaList.get(position).getNamaKota());
}
@Override
public int getItemCount() {
return mKotaList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView mTextViewNamaKota;
public MyViewHolder(View itemView) {
super(itemView);
mTextViewNamaKota = (TextView) itemView.findViewById(R.id.tvNamaKota);
}
}
Activity
KotaApiInterface mApiInterface;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
public static KotaActivity ka;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kota);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mApiInterface = ApiClient.getClient().create(KotaApiInterface.class);
ka=this;
refresh();
}
public void refresh() {
Call<GetKota> kotaCall = mApiInterface.getKota();
kotaCall.enqueue(new Callback<GetKota>() {
@Override
public void onResponse(Call<GetKota> call, Response<GetKota>
response) {
List<Kota> KotaList = response.body().getListDataKota();
Log.d("Retrofit Get", "Jumlah data Kota: " +
String.valueOf(KotaList.size()));
mAdapter = new KotaAdapter(KotaList);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onFailure(Call<GetKota> call, Throwable t) {
Log.e("Retrofit Get", t.toString());
}
});
}
There's no error but the list shows nothing. I'm new in rest api can somebody help me? sorry for the long code. thanks in advance
> getKota();` change your interface like this and make necessary changes in code
– Aldrin Joe Mathew Apr 25 '17 at 09:46