简体   繁体   English

Laravel 4 REST API在Android Studio中使用Retrofit

[英]Laravel 4 REST API using Retrofit in Android Studio

I'm trying to show some list in android using retrofit. 我正在尝试使用改造在android中显示一些列表。

I'm using rest api laravel with url http://ecinema.esy.es/public/kota 我正在将REST API laravel与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 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 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 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? 我是Rest API的新手,有人可以帮助我吗? 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 " 错误很简单: “ RecyclerView:未连接适配器;跳过布局和翻新获取:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第2列路径处为BEGIN_ARRAY”

It says that your code is expecting an object however what you are getting in Json response is an array of Objects. 它说您的代码期望一个对象,但是您在Json响应中得到的是一个对象数组。 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 由于您尚未上传模型类(GetKota和Kota),因此我认为您的Kota类应该是

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 尝试在ApiInterface中执行类似的操作

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

and get KotaList as 并获得KotaList为

List<Kota> KotaList = response.body();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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