【问题标题】:Laravel 4 REST API using Retrofit in Android Studio在 Android Studio 中使用 Retrofit 的 Laravel 4 REST API
【发布时间】: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&lt;List&lt;Kota&gt;&gt; getKota(); 像这样更改您的界面并在代码中进行必要的更改
  • 你得到这个错误是因为你实际上得到的是一个 'Kota' 的数组,但是你试图将它转换成一个对象

标签: android json laravel rest api


【解决方案1】:

错误很简单: “RecyclerView:未连接适配器;跳过布局和改造获取:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期 BEGIN_OBJECT 但在第 1 行第 2 列路径为 BEGIN_ARRAY”

它表示您的代码需要一个对象,但是您在 Json 响应中得到的是一个对象数组。所以你需要做的就是将它序列化为对象数组。

由于您尚未上传模型类(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;
    }}

尝试在 ApiInterface 中做这样的事情

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

并获取 KotaList 为

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

【讨论】:

  • List KotaList = response.body(); 上的类型不兼容;必需:列表 找到:列表
  • 再次检查您的 ApiInterface 类
  • @GET("kota") Call&lt;List&lt;Kota&gt;&gt; getKota();
  • 我把它改成了@GET("kota") Call> getKota();
  • 您的 Get quota 类尝试使用的内容类似于 { "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" } ] }
猜你喜欢
  • 1970-01-01
  • 2021-05-18
  • 2014-08-07
  • 2020-12-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-30
  • 2020-06-25
  • 1970-01-01
相关资源
最近更新 更多