无法使用改造获取数据

Unable to get data using retrofit

您好,我正在尝试使用改装但我收到此错误:-

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

这是我的 MainActivity

public class MainActivity extends AppCompatActivity {

@Bind(R.id.activity_main_tv_display)
TextView textData;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
}
@OnClick(R.id.activity_main_btn_show)
void press() {
    RemoteApi.Factory.getInstance().getModel().enqueue(new Callback<Model>() {
        @Override
        public void onResponse(Call<Model> call, Response<Model> response) {
            textData.setText(response.toString());
            Log.e("--success--", String.valueOf(response));
        }
        @Override
        public void onFailure(Call<Model> call, Throwable t) {
            Log.e("--fail--", t.getMessage());
        }
    });
  }
}

这是我的模型

public class Model {

@SerializedName("Title")
@Expose
private String Title;
@SerializedName("Message")
@Expose
private String Message;
@SerializedName("id")
@Expose
private int id;
// getters and setters declare
}

这是我的界面

public interface RemoteApi {

String BASE_URL = "xyz/";
@GET("api/Cards")
Call<Model> getModel();
class Factory {
    public static RemoteApi remoteApi;
    public static RemoteApi getInstance() {
            Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())
                    .baseUrl(BASE_URL)
                    .build();
            remoteApi = retrofit.create(RemoteApi.class);
            return remoteApi;
        }
    }
}

我的 API 看起来像这样

[{
  "Title": "xyz",
  "Message": "hello",
  "id": 1
}, {
  "Title": "abc",
  "Message": "hello",
  "id": 2
}] 

您想从 API 中获取对象列表(通过查看第一个字符),但您的错误表明它需要一个对象(也通过查看第一个字符)。您在界面中使用了 Call<Model>,这说明您只希望返回一个 Model 对象,而不是它们的列表。

尝试像这样设置你的界面

public interface RemoteApi {

    String BASE_URL = "xyz/";
    @GET("api/Cards")
    Call<List<Model>> getModel();

其他代码也是这样

RemoteApi.Factory.getInstance().getModel().enqueue(new Callback<List<Model>>() {
        @Override
        public void onResponse(Call<List<Model>> call, Response<List<Model>> response) {
            String responseString = String.valueOf(response);
            textData.setText(responseString);
            Log.e("--success--", responseString);
        }
        @Override
        public void onFailure(Call<List<Model>> call, Throwable t) {
            Log.e("--fail--", t.getMessage());
        }
    });