【发布时间】:2021-08-28 01:47:41
【问题描述】:
我正在做 ecg 监控 android 应用程序的项目,所以我需要从 ubidots 获取实时数据,当我运行应用程序时我设法获取最新的变量值,但不是所有将不断更新的新值。所以,我需要有助于在我的应用中获取实时数据,因此我的应用应该在 ubidots 变量中更新时获取数据。
这是我获取变量的代码:
public class MainActivity extends Activity {
private TextView ecgLevel;
String variableValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ecgLevel = (TextView) findViewById(R.id.ecg);
ApiUbidots getApi = new ApiUbidots();
getApi.execute();
}
class ApiUbidots extends AsyncTask<Integer, Void, Value[]> {
private final String API_KEY = “";
private final String VARIABLE_ID = "”;
@Override
protected Value[] doInBackground(Integer... params) {
ApiClient apiClient = new ApiClient(API_KEY);
Variable ecgLevel = apiClient.getVariable(VARIABLE_ID);
Value[] variableValues = ecgLevel.getValues();
return variableValues;
}
@Override
protected void onPostExecute(Value[] variableValues) {
double varValue = variableValues[0].getValue();
variableValue = (String.valueOf(varValue));
ecgLevel.setText(variableValue);
}
}
}```
【问题讨论】: