【问题标题】:Expected BEGIN_ARRAY but was BEGIN_OBJECT Android Json应为 BEGIN_ARRAY 但为 BEGIN_OBJECT Android Json
【发布时间】:2016-06-13 16:26:54
【问题描述】:

我正在尝试在我的应用中实施 Retrofit 以与我的服务器端点进行通信。我在后端有一个名为“Feature”的表,其中包含 id: number、name: text 和 roomID:number 字段。我不明白我做错了什么。我严格遵循改造文档。当我尝试从数据库中按 ID 获取特定功能时出现此错误,我收到此错误:

IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
06-13 17:21:20.896 15174-15180/xdesign.georgi.espc_retrofit W/art: Suspending all threads took: 5.681ms

这是我的主要活动:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            GitHubService gitHubService = GitHubService.retrofit.create(GitHubService.class);


            final Call<Feature> call = gitHubService.repoContributors(173);

            call.enqueue(new Callback<Feature>() {
                @Override
                public void onResponse(Call<Feature> call, Response<Feature> response) {
                    Log.e("MainActivity","onResponce");
                    final TextView textView = (TextView) findViewById(R.id.textView);
                    textView.setText(response.body().toString());
                }

                @Override
                public void onFailure(Call<Feature> call, Throwable t) {
                    Log.e("MainActivity","onFailure" + t.toString());
                    final TextView textView = (TextView) findViewById(R.id.textView);
                    textView.setText("Something went wrong: " + t.getMessage());
                }


            });
        }
    });
}

GitHutService.java

interface GitHubService {
@GET("Features/{id}")
Call<Feature> repoContributors(@Path("id") int id);


public static final Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://10.0.2.2:3000/api/")
        .addConverterFactory(GsonConverterFactory.create())
        .build();
}

Feature.java

public class Feature {

String name;
int roomID;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getRoomID() {
    return roomID;
}

public void setRoomID(int roomID) {
    this.roomID = roomID;
}

@Override
public String toString() {
    return "Feature{" +
            "name='" + name + '\'' +
            ", roomID=" + roomID +
            '}';
}
}

注意:这个问题可能发生在任何解析的网络库中 JSON 响应并返回最终的 Java POJO。

【问题讨论】:

    标签: android gson android-volley retrofit square


    【解决方案1】:

    我认为您的服务器会响应一系列 Feature 对象,但您已将其作为您的 Pojo 给出,这导致了问题。

    尝试使用将用作 JSON 的 pojo 的泛型类,您的问题应该得到解决。

    【讨论】:

    • 感谢您的建议!我会看看它是否能解决问题:)
    • 欢迎。我将编辑您的问题,因为任何在 POJO 中提供响应的库都会发生这种情况:)
    【解决方案2】:

    您的对象类未映射为json响应,使用Postman获取响应并粘贴响应here以获取对象中的正确参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-22
      • 2020-03-28
      • 2018-01-12
      • 2018-04-24
      • 2016-12-27
      • 2013-08-10
      相关资源
      最近更新 更多