0

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

Sekar Anindya
  • 83
  • 2
  • 13

1 Answers1

0

Error is simple: "RecyclerView: No adapter attached; skipping layout and Retrofit Get: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path "

It says that your code is expecting an object however what you are getting in Json response is an array of Objects. So what you need to do is serialize it in array of Objects.

Since you have not uploaded the model classes (GetKota and Kota) what i believe your Kota class should look like is

public class Kota{

    private String idKota;
    private String namaKota;
    private String flag;
    private String created_at;
    private String updated_at;

    @Override
    public String toString() {
        return idKota+namaKota+flag+created_at+updated_at;
    }}

Try doing something like this in ApiInterface

@GET("kota")
Call<List<Kota>> getKota();

and get KotaList as

List<Kota> KotaList = response.body();
Ahsan Iqbal
  • 316
  • 1
  • 5
  • incompatible types on List KotaList = response.body(); required: List found: List – Sekar Anindya Apr 25 '17 at 10:24
  • Check your ApiInterface class again – Ahsan Iqbal Apr 25 '17 at 10:34
  • `@GET("kota") Call> getKota();` – Ahsan Iqbal Apr 25 '17 at 10:34
  • I've changed it to @GET("kota") Call> getKota(); – Sekar Anindya Apr 25 '17 at 10:42
  • what your Get quota class is trying to consume is something like `{ "status":"some status", "message":"some message" "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" } ] }` – Ahsan Iqbal Apr 25 '17 at 10:50
  • but i dont see that in the response. What you are getting in response is just array of Kota no success or message in Json. All you need to do is change `@GET("kota") Call> getKota();` to `@GET("kota") Call> getKota();` – Ahsan Iqbal Apr 25 '17 at 10:53
  • There is no need of GetKota class – Ahsan Iqbal Apr 25 '17 at 10:54
  • thank you i just realised that. but when i changed the getKota to Kota the error RecyclerView: No adapter attached; skipping layout stil not solved and the list stil shows nothing – Sekar Anindya Apr 25 '17 at 10:58
  • Try adding an adapter in onCreate `@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; mAdapter = new KotaAdapter(new ArrayList()); mRecyclerView.setAdapter(mAdapter); refresh(); }` – Ahsan Iqbal Apr 25 '17 at 11:18
  • [http://stackoverflow.com/questions/29141729/recyclerview-no-adapter-attached-skipping-layout](http://stackoverflow.com/questions/29141729/recyclerview-no-adapter-attached-skipping-layout) This issue is already addressed here – Ahsan Iqbal Apr 25 '17 at 11:20
  • Thank you so much you saved my life – Sekar Anindya Apr 25 '17 at 11:26