【问题标题】:Unable to get data using retrofit无法使用改造获取数据
【发布时间】:2016-03-22 18:38:50
【问题描述】:

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

java.lang.IllegalStateException: 应为 BEGIN_OBJECT 但在第 1 行第 2 列路径 $ 处为 BEGIN_ARRAY

这是我的 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 正在返回一个 JSON 数组。改造期待Callback&lt;Model&gt;。试试Callback&lt;List&lt;Model&gt;&gt;
  • 你能解释更多@cricket_007吗
  • {} 中的数据是一个 JSON 对象。 [] 中的数据是一个 JSON 数组。查看 API 的第一个字符。这是一个[。因此,您的错误报告是“was BEGIN_ARRAY”,而不是“BEGIN_OBJECT”。
  • 但是当我将我的界面从 Call getModel() 编辑到 Callback> getModel() 时。它在我的 MainActivity 中创建错误,即无法解析方法 enqueue @cricket_007
  • 那是因为您必须在您拥有CallbackCall 的所有地方将Model 更改为List&lt;Model&gt;

标签: android json gson retrofit2


【解决方案1】:

您想从 API 中获取对象列表(通过查看第一个字符),但您的错误表明它需要一个对象(同样,通过查看第一个字符)。您在界面中使用Call&lt;Model&gt;,这表明您只希望返回一个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());
        }
    });

【讨论】:

  • 但它是 display retrofit2.Response@41ce5630 而不是结果@cricket_007
  • 是的,那是因为你在做String.valueOf(response)。试试response.body() 而不是response
  • Log.e 正在显示 [examples.sewoyebah.com.retrofit.model.Model@41cfd158,examples.sewoyebah.com.retrofit.model.Model@41c5ee70]
  • 是的,这是您的模型对象的列表。请参考How do I print my Java object without getting “SomeType@2f92e0f4”?
猜你喜欢
  • 2018-04-20
  • 1970-01-01
  • 1970-01-01
  • 2020-08-14
  • 2016-05-09
  • 2018-12-29
  • 2019-07-05
  • 2019-02-06
  • 1970-01-01
相关资源
最近更新 更多