简体   繁体   中英

Laravel 4 REST API using Retrofit in Android Studio

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

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM