获取 GsonConverterFactory 来解析某些字段?
Get GsonConverterFactory to parse certain fields?
鉴于以下 JSON 响应:
{
"status":true,
"doc":[
{
"_id":"9374",
"t_id":"5678",
"name":"Do calculus homework",
"description":"Finish all assigned homework from chapters 1 and 2",
"category":"test",
"indexInList":0,
"priority":3,
"dateDue":1477291500000,
"user":"def",
"status":"ARCHIVED",
"__v":0,
"subtasks":[
{
"name":"Finish Chapter 1 - Derivatives",
"isCompleted":false
},
{
"name":"Finish Chapter 1 - Integrals",
"isCompleted":false
},
{
"name":"Finish Chapter 2 - Graphing",
"isCompleted":false
}
]
},
{
"_id":"429808",
"t_id":"1234",
"name":"Write machine learning essay",
"description":"Write essay on Bayesian networks",
"category":"test",
"indexInList":1,
"priority":3,
"dateDue":1477291500000,
"user":"abc",
"status":"ARCHIVED",
"__v":0,
"subtasks":[
{
"name":"Write introduction",
"isCompleted":false
},
{
"name":"Write body",
"isCompleted":false
},
{
"name":"Write Conclusion",
"isCompleted":false
}
]
}
]
}
我将其与 Retrofit2 结合使用。我的服务 class 看起来像这样:
private HavocService(String baseUrl) {
//So network calls are async
RxJavaCallAdapterFactory rxAdapter = RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io());
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(client)
.addCallAdapterFactory(rxAdapter)
.addConverterFactory(GsonConverterFactory.create())
.build();
mHavocApi = retrofit.create(HavocAPI.class);
}
我实际上正在处理在 rx 任务中获取该数据:
rxHelper.manageSubscription(HavocService.getInstance().getHavocAPI().getAllTasks(userId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.compose(RxTiPresenterUtils.deliverLatestToView(this))
.subscribe(mListOfTasks -> {
this.mListOfTasks = mListOfTasks;
getView().setTaskList(mListOfTasks);
}, throwable -> {
LogUtil.e("Error with something.");
})
);
如何让我的 GSONConverterFactory 在 "doc"
数组处开始解析?我不太关心第一个 "status"
字段。
我问是因为我收到以下错误:java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
。我很确定这是因为 GSON 正在尝试解析第一项而不是到达 "doc"
数组。
感谢所有帮助!
编辑
这是 HavocAPI (我只关心 getAllTasks()
目前的工作情况。)
public interface HavocAPI {
/**
* Creates a new Task
*
* @return status of whether or not the transaction was successful and the task that was created
*/
@Headers({"Accept: application/json", "Content-Type: application/json"})
@POST("task/create/")
Observable<List<Object>> createNewTask();
/**
* Deletes a specified Task using the taskId
*
* @param taskID of the task
* @return status of transaction
*/
@Headers({"Accept: application/json", "Content-Type: application/json"})
@POST("task/delete/{task_id}/")
Observable<Boolean> deleteTask(@Path("task_id") String taskID);
/**
* Updates a Task
*
* @return status of whether or not the transaction was successful and the task that was updated
*/
@Headers({"Accept: application/json", "Content-Type: application/json"})
@POST("task/update/")
Observable<List<Object>> updateTask();
/**
* Gets all Tasks by a specified User
*
* @param userId of the user
* @return list of all Tasks from the specified User
*/
@Headers({"Accept: application/json", "Content-Type: application/json"})
@GET("task/read/{user_id}/")
Observable<List<Task>> getAllTasks(@Path("user_id") String userId);
}
getAllTasks()
方法的 return 类型不正确。您需要创建一个表示实际响应格式的新模型,然后通过它访问任务列表。
class GetTasksResponse {
bool status;
@SerializedName("doc")
List<Task> tasks;
public List<Task> getTasks() {
return tasks;
}
}
鉴于以下 JSON 响应:
{
"status":true,
"doc":[
{
"_id":"9374",
"t_id":"5678",
"name":"Do calculus homework",
"description":"Finish all assigned homework from chapters 1 and 2",
"category":"test",
"indexInList":0,
"priority":3,
"dateDue":1477291500000,
"user":"def",
"status":"ARCHIVED",
"__v":0,
"subtasks":[
{
"name":"Finish Chapter 1 - Derivatives",
"isCompleted":false
},
{
"name":"Finish Chapter 1 - Integrals",
"isCompleted":false
},
{
"name":"Finish Chapter 2 - Graphing",
"isCompleted":false
}
]
},
{
"_id":"429808",
"t_id":"1234",
"name":"Write machine learning essay",
"description":"Write essay on Bayesian networks",
"category":"test",
"indexInList":1,
"priority":3,
"dateDue":1477291500000,
"user":"abc",
"status":"ARCHIVED",
"__v":0,
"subtasks":[
{
"name":"Write introduction",
"isCompleted":false
},
{
"name":"Write body",
"isCompleted":false
},
{
"name":"Write Conclusion",
"isCompleted":false
}
]
}
]
}
我将其与 Retrofit2 结合使用。我的服务 class 看起来像这样:
private HavocService(String baseUrl) {
//So network calls are async
RxJavaCallAdapterFactory rxAdapter = RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io());
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(client)
.addCallAdapterFactory(rxAdapter)
.addConverterFactory(GsonConverterFactory.create())
.build();
mHavocApi = retrofit.create(HavocAPI.class);
}
我实际上正在处理在 rx 任务中获取该数据:
rxHelper.manageSubscription(HavocService.getInstance().getHavocAPI().getAllTasks(userId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.compose(RxTiPresenterUtils.deliverLatestToView(this))
.subscribe(mListOfTasks -> {
this.mListOfTasks = mListOfTasks;
getView().setTaskList(mListOfTasks);
}, throwable -> {
LogUtil.e("Error with something.");
})
);
如何让我的 GSONConverterFactory 在 "doc"
数组处开始解析?我不太关心第一个 "status"
字段。
我问是因为我收到以下错误:java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
。我很确定这是因为 GSON 正在尝试解析第一项而不是到达 "doc"
数组。
感谢所有帮助!
编辑
这是 HavocAPI (我只关心 getAllTasks()
目前的工作情况。)
public interface HavocAPI {
/**
* Creates a new Task
*
* @return status of whether or not the transaction was successful and the task that was created
*/
@Headers({"Accept: application/json", "Content-Type: application/json"})
@POST("task/create/")
Observable<List<Object>> createNewTask();
/**
* Deletes a specified Task using the taskId
*
* @param taskID of the task
* @return status of transaction
*/
@Headers({"Accept: application/json", "Content-Type: application/json"})
@POST("task/delete/{task_id}/")
Observable<Boolean> deleteTask(@Path("task_id") String taskID);
/**
* Updates a Task
*
* @return status of whether or not the transaction was successful and the task that was updated
*/
@Headers({"Accept: application/json", "Content-Type: application/json"})
@POST("task/update/")
Observable<List<Object>> updateTask();
/**
* Gets all Tasks by a specified User
*
* @param userId of the user
* @return list of all Tasks from the specified User
*/
@Headers({"Accept: application/json", "Content-Type: application/json"})
@GET("task/read/{user_id}/")
Observable<List<Task>> getAllTasks(@Path("user_id") String userId);
}
getAllTasks()
方法的 return 类型不正确。您需要创建一个表示实际响应格式的新模型,然后通过它访问任务列表。
class GetTasksResponse {
bool status;
@SerializedName("doc")
List<Task> tasks;
public List<Task> getTasks() {
return tasks;
}
}