To work with cache by device parameters, create the CashByParamActivity application, which uses the methods implemented in the RequestAPI and DatabaseAPI units.
Before creating an application make sure that the resource is available in PostgreSQL data source. The table consists of three columns containing information about fruits: id, name, description.
The application sends request to the fruits_get and fruits_insert_delete tables that address the same database and table at remote PostgeSQL server.. The fruits_get table requests and saves data on a mobile device. The fruits_insert_delete table removes and adds rows at the remote server. The response contains the refreshed table.
Saving data depends on sent parameters:
If equal parameters are sent or there are not parameters, data is saved to one table.
If different parameters are sent, data is saved to one table with table and parameter hash name.
The CashByParamActivity application includes one screen, text input area and buttons:
TableStreamWithoutParam. Add a table.
TableStreamInsert. Add a row to table.
TableStreamDelete. Delete a row from table.
GetTableName. Get table name.
DropCache. Delete table.
The text input area contains the 10001 row identifier by default.
To view application work results, use the interactive example:
NOTE. Save the database from the mobile device to the computer and use SQLiteStudio to control operations.
Click the TableStreamWithoutParam button.
public class CashByParamActivity extends AppCompatActivity {
private static final String MY_URL = "http://testmasterfmp.fsight.cloud/";
private static final VersionAPI MY_VERSION_API = VersionAPI.V_1;
private static final String MY_ENVIRONMENT = "Leonid_environment";
private static final String MY_PROJECT = "Leonid_project";
private static final String MY_VERSION = "v1";
private static final String MY_LOGIN = "Leonid";
private static final String MY_PASSWORD = "123123";
private static final String TAG = "CashByParamActivity";
private static final String MY_RESOURCE_FRUITS_GET = "fruits_get";
private static final String MY_RESOURCE_FRUITS_INSERT_DELETE = "fruits_insert_delete";
private HyperHive hyperHive;
private TextView textView;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cash_by_param);
textView = findViewById(R.id.text_view_cash_by_param);
editText = findViewById(R.id.edit_text_cash_by_param);
editText.setText("10001");
Handler uiHandler = new Handler();
hyperHive = new HyperHiveState(getApplicationContext())
.setHostWithSchema(MY_URL)
.setApiVersion(MY_VERSION_API)
.setEnvironmentSlug(MY_ENVIRONMENT)
.setProjectSlug(MY_PROJECT)
.setVersionProject(MY_VERSION)
.setHandler(uiHandler)
.buildHyperHive();
auth();
}
public void auth() {
String status = hyperHive.authAPI.auth(MY_LOGIN, MY_PASSWORD, true).execute().toString();
showDate(status);
}
public void tableStreamWithoutParam(View view) {
String baseStatus = hyperHive.requestAPI.tableStream(MY_RESOURCE_FRUITS_GET).execute();
showDate(baseStatus);
}
public void tableStreamInsert(View view) {
TableStreamCallParams tableStreamCallParams = new TableStreamCallParams();
String str = editText.getText().toString();
String json = "{\"upsert_rows\": [[\"" + str + "\", \"apple" + str + "\"]], \"delete_ids\": null}";
tableStreamCallParams.setData(json);
Log.d(TAG, "tableStreamInsert: " + json);
BaseStatus baseStatus = hyperHive.requestAPI.tableStream(MY_RESOURCE_FRUITS_INSERT_DELETE, tableStreamCallParams, BaseStatus.class).execute();
showDate(baseStatus.toString());
Log.d(TAG, "tableStreamInsert: " + baseStatus.isOk());
}
public void tableStreamDelete(View view) {
TableStreamCallParams tableStreamCallParams = new TableStreamCallParams();
String str = editText.getText().toString();
String json = "{\"upsert_rows\": null, \"delete_ids\": [[\"" + str + "\"]]}";
tableStreamCallParams.setData(json);
Log.d(TAG, "tableStreamDelete: " + json);
BaseStatus baseStatus = hyperHive.requestAPI.tableStream(MY_RESOURCE_FRUITS_INSERT_DELETE, tableStreamCallParams, BaseStatus.class).execute();
showDate(baseStatus.toString());
Log.d(TAG, "tableStreamDelete: " + baseStatus.isOk());
}
public void getTableName(View view) {
String str = editText.getText().toString();
String json = "{\"upsert_rows\": [[\"" + str + "\", \"apple" + str + "\"]], \"delete_ids\": null}";
RootClass rootClass = hyperHive.databaseAPI.getTablesName(MY_RESOURCE_FRUITS_INSERT_DELETE, json, RootClass.class).execute();
showDate(rootClass.resultClass.databaseClass.recordsClass.get(0).name);
}
public void dropCache(View view) {
String str = editText.getText().toString();
String json = "{\"upsert_rows\": [[\"" + str + "\", \"apple" + str + "\"]], \"delete_ids\": null}";
String status = hyperHive.databaseAPI.dropCache(MY_RESOURCE_FRUITS_INSERT_DELETE, json).execute();
showDate(status);
}
private void showDate(String status) {
Log.d(TAG, "showDate: " + status);
textView.setText(status);
}
class RootClass extends BaseStatus {
@Expose
@SerializedName("result")
ResultClass resultClass;
class ResultClass {
@Expose
@SerializedName("database")
DatabaseClass databaseClass;
}
class DatabaseClass {
@Expose
@SerializedName("records")
ListrecordsClass;
}
class RecordsClass {
@Expose
@SerializedName("name")
String name;
}
}
}
See also:
Examples of Android Framework Use | Examples of Working with Resources