Developer Documentation Library > Mobile SDK > Disconnected mobile application reference > Expression Evaluator reference > Expression evaluation for Android
 
Expression evaluation for Android
An Android application can use a WebView object that hosts HTML and JavaScript. The Android application can then use the WebView object to run JavaScript statements.
The Android application can open an empty web page that references only the JavaScript source files for the Categorical, InterviewValue, FunctionLibrary, and Expression classes. The application can evaluate expressions by creating short JavaScript statements that use the supplied JavaScript classes. The application can then run the statements with the WebView.loadUrl() method.
"NumCats + NumDogs > 4"
The Routing XML (see XML schema definitions) includes both the text expression, and the opcodes that correspond to the text expression For example, the following code is supplied in the Routing XML:
<Opcodes>
  <Opcode Code="PushIdentifier">
    <Operand>NumCats</Operand>
  </Opcode>
  <Opcode Code="PushIdentifier">
    <Operand>NumDogs</Operand>
  </Opcode>
  <Opcode Code="OpAdd" />
  <Opcode Code="PushNumeric">
    <Operand>4</Operand>
  </Opcode>
  <Opcode Code="CmpGtr" />
</Opcodes>
When the mobile application encounters the expression, on a Routing Item, when it conducts the interview, it must supply values for the NumCats and NumDogs identifiers. The application must also evaluate whether the expression is True or False.
The Opcodes XML, must first be converted into the required JSON format. The following example provides the conversion results:
[
{
"Code":"PushIdentifier",
"Operands":[
"NumCats"
]
},
{
"Code":"PushIdentifier",
"Operands":[
"NumDogs"
]
},
{
"Code":"OpAdd"
},
{
"Code":"PushNumeric",
"Operands":[
"4"
]
},
{
"Code":"CmpGtr"
}
]
Next, an Array of identifier objects (that provide values for each expression identifier) must be created. For example, if the answer to the NumCats question is 2, and the answer to the NumDogs question is 1, the mobile application creates the following array (represented as JSON):
[
{
"name":"NumCats",
"value":"2"
},
{
"name":"NumDogs",
"value":"1"
}
]
The final step is to build the full set of JavaScript statements to be run.
String JavaScript = "JavaScript: ";
JavaScript += "var expression = new Expression(" + opcodeJSON + ");";
JavaScript += "var result = expression.evaluate(" + identifierJSON + ")";
JavaScript += "window.JSInterface.showResult(result.toString()); ";
The three JavaScript statements, run sequentially, construct an Expression object from the supplied opcode information, and call evaluate on the Expression object (using the supplied values). The results are returned to the Java code. In this example, the JavaScript returns the results to the Java code by calling the showResult method on a JSInterface object. The Java code exposes the object to WebView by usingW ebView::addJavaScriptInterface().
The Java code evaluates the expression by calling the following script:
webView.loadUrl(JavaScript);
The script causes WebView to run the JavaScript statements, which then calls theshowResult method with the result.
The example hardcoded the identifier JSON with the NumCats and NumDogs identifier names. In practice, the mobile application must first call expression.getIdentifiers() to determine the identifiers, retrieve the associated values, assign the values to the identifiers, and finally call expression.evaluate().
See also
Expression Evaluator reference