我是新手进行改造,并且不知道如何使用改造传递这个json并在我的应用程序中显示.Below是我的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;
}
}
答案 0 :(得分:2)
创建自定义模型并在改造API中传递相同的模型。
class Model{
List<ObjectModel> objectmodel;
}
class ObjectModel{
String userId;
String id;
String title;
String body;
}
绑定值并在改装界面中传递Model对象。
或者
@GET("json")
Call<JsonObject> youmethode(@Query("key") String value);
希望这有帮助。感谢
答案 1 :(得分:0)
要传递json请求,您必须使用post方法。