【发布时间】:2017-09-22 05:54:45
【问题描述】:
我正在尝试使用改造在 android 中显示一些列表。
我正在使用带有 url http://ecinema.esy.es/public/kota 的 rest api laravel
结果
[
{
"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;
}
API接口
@GET("kota")
Call<GetKota> getKota();
适配器
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);
}
}
活动
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());
}
});
}
没有错误,但列表没有显示任何内容。我是rest api的新手,有人可以帮助我吗?对不起,长代码。在此先感谢
【问题讨论】:
-
您的日志是否显示 JSON 响应?
-
@AldrinMathew 否,但它显示 RecyclerView:未连接适配器;跳过布局和改造获取:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期 BEGIN_OBJECT 但在第 1 行第 2 列路径 $ 处为 BEGIN_ARRAY
-
添加一个日志拦截器来验证你的改造请求和响应stackoverflow.com/q/32514410/7743702
-
@GET("kota") Call<List<Kota>> getKota();像这样更改您的界面并在代码中进行必要的更改 -
你得到这个错误是因为你实际上得到的是一个 'Kota' 的数组,但是你试图将它转换成一个对象
标签: android json laravel rest api