我正在学习 Android,我的任务是使用 Google Fit steps API 检索步数数据。但是,我不断收到“任务尚未完成”错误。我不知道这是我那部分的代码:private List<Fitness> readHistoryDataFromGoogle() { DataReadRequest readRequest = createDataReadRequest(); Task<DataReadResponse> response = com.google.android.gms.fitness.Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this)).readData(readRequest); DataReadResponse responses = response.getResult(); List <Fitness> fitnessItems = parseFitness(responses); return fitnessItems;}private List<Fitness> parseFitness(DataReadResponse dataReadResponse) { List<com.example.mentalhealthapp.Fitness> fitnessItems = new ArrayList<>(); for (Bucket bucket : dataReadResponse.getBuckets()) { for (DataSet set : bucket.getDataSets()) { for (DataPoint dataPoint : set.getDataPoints()) { int numSteps = dataPoint.getValue(Field.FIELD_STEPS).asInt(); long startDateTime = dataPoint.getStartTime(TimeUnit.MILLISECONDS); long endDateTime = dataPoint.getEndTime(TimeUnit.MILLISECONDS); fitnessItems.add(new com.example.mentalhealthapp.Fitness(numSteps, startDateTime, endDateTime)); } } } return fitnessItems;}非常感谢您的帮助!
1 回答
繁星点点滴滴
TA贡献1803条经验 获得超3个赞
你遇到了这个问题,因为 API 请求(服务调用)是Asynchronous
,它们不会立即完成并返回数据,你不能像其他代码一样逐行运行它们。您必须等待该响应完成,然后才能访问它返回的数据。从文档中它告诉你它是一个 REST api:
添加回答
举报
0/150
提交
取消