【发布时间】:2017-02-23 04:54:14
【问题描述】:
我是改造新手,不太了解如何使用改造传递此 json 并在我的应用程序中显示。下面是我的 json、MainActivity 和模型类。但知道它抛出异常“除了 begin_object 但找到 begin_array “。请帮帮我。
[
{
"userId": 1,
"id": 1,
"title": "sunt",
"body": "sdfdsf"
},
{
"userId": 1,
"id": 2,
"title": "qui est esse",
"body": "jhmjk"
}]
MainActivity.java
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiInterface apiService = retrofit.create(ApiInterface.class);
// ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<ModelClass> call = apiService.getLogin();
// Call<LoginResponse> call = apiService.loginWithCredentials(new LoginRequest(email_enter, md5pass));
call.enqueue(new Callback<ModelClass>() {
@Override
public void onResponse(Call<ModelClass> call, Response<ModelClass> response) {
Log.i("REGISTRATION --->", "Registered" + response);
}
@Override
public void onFailure(Call<ModelClass> call, Throwable t) {
Log.i("REGISTRATION --->", "Throwable" + t.toString());
// Intent i = new Intent(MainActivity.this,SecondActivity.class);
// startActivity(i);
}
});
}
ModelClass.java
public class ModelClass {
class Model{
public List<ObjectModel> getObjectmodel() {
return objectmodel;
}
public void setObjectmodel(List<ObjectModel> objectmodel) {
this.objectmodel = objectmodel;
}
List<ObjectModel> objectmodel;
}
class ObjectModel{
String userId;
String id;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
String title;
String body;
}
}
【问题讨论】:
-
你能看看这个链接,有json,如何通过stackoverflow.com/questions/42386604/…
-
我建议将正文中的 JSON 数据作为 POST 请求传递