我在尝试构建 google api 客户端以访问 fit api 时手机未连接到互联网时出现以下错误:connection failed. Cause: ConnectionResult{statusCode=NETWORK_ERROR, resolution=null, message=null}
。我尝试了两种与 google fit api 连接的方法:
方法一:登录后连接Fit API,无网络时此方法无效。我认为登录过程需要互联网。
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestScopes(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE), new Scope(Scopes.FITNESS_LOCATION_READ))
.build();
return new GoogleApiClient.Builder(activity)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addConnectionCallbacks(connectionCallbacks)
.addOnConnectionFailedListener(failedListener)
.addApi(Fitness.HISTORY_API)
.addApi(Fitness.SESSIONS_API)
.addApi(Fitness.RECORDING_API)
.addApi(Fitness.SENSORS_API)
.enableAutoManage(this, 0, this)
.build();
方法2:因此我尝试删除登录功能,因为我觉得一旦用户登录并授权fit api收集数据,那么登录就不是必需的了。
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.HISTORY_API)
.addApi(Fitness.SESSIONS_API)
.addApi(Fitness.RECORDING_API)
.addApi(Fitness.SENSORS_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(failedListener)
.build();
但是我在连接失败的侦听器中收到此错误ConnectionResult{statusCode=SIGN_IN_FAILED, resolution=null, message=null}
。
如何确保在没有互联网连接的情况下可以访问 fitApi?