0

我正在尝试在我的应用程序中实施改造以与我的服务器端点进行通信。我在后端有一个名为“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());
                }


            });
        }
    });
}

GitHubService.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 的网络库中。

4

2 回答 2

1

我认为您的服务器以 Feature 对象数组进行响应,但您已将其作为导致问题的 Pojo 给出。

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

于 2016-06-13T16:37:50.763 回答
0

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

于 2016-06-13T16:39:02.187 回答