1

I'm working on an Android app "native written in java" and I'm getting a response from a server the response is a javascript function

I need to use this function to do some calculations inside my native java code.

any ideas how to do so.

sample response :

function logic_1(surveyString, responseValuesString) { 
    var survey = eval(surveyString);
    var responseValues = eval(responseValuesString);
    var target = new Object();
if (isChosen(128133225, responseValues)) { 
target.id = 2;
}
if (! target.id) { 
    target.id = 2;
 } 
    return target;
 } 
5
  • Maybe the response could be the result of the function, or a code indicating a java function to run? Commented Mar 28, 2012 at 9:18
  • what response are you getting Commented Mar 28, 2012 at 9:21
  • the response is a javascript function Commented Mar 28, 2012 at 9:23
  • i want to use this JS code inside my java code Commented Mar 28, 2012 at 9:25
  • Really suggest to change how the server is responding in case you had access to that. It's a bad habit to return programming-specific data to the client. Commented Apr 1, 2012 at 21:05

3 Answers 3

3

I've previously used Rhino successfully to execute JavaScript code on Android: http://www.mozilla.org/rhino/

Sign up to request clarification or add additional context in comments.

4 Comments

I'm getting response as a function that takes 2 input parameters .. can I give this function input from my java code ? I mean "function logic_1(surveyString, responseValuesString)" any way to set those 2 parameters from Java ??
Yes, this is not a problem with the Rhino APIs. This answer describes how to do it: stackoverflow.com/a/3996115/511012
works perfect ! thanks okay just another question ... you can see that the function is returning an object var target = new Object(); I was able to catch an integer, boolean, string in the return but how to catch an object ?
I've posted an example as a separate answer, so I could include code.
1

Here's an example of how to return values from a complex type:

String strFunction = 
        "function add(x,y){ " +
            "return { " +
                "id:x+y " +
            "}; " +
        "}";

Context context = Context.enter();
ScriptableObject scope = context.initStandardObjects();
context.evaluateString(scope, strFunction, "test", 1, null);

Function functionAdd = (Function)scope.get("add");
NativeObject untypedResult = (NativeObject)functionAdd.call(context, scope, scope, new Object[] { 1, 2 });
double id = (Double)untypedResult.get("id", untypedResult);

The important part is the last two lines, where we call the JavaScript function, treat the result as a NativeObject, and then retrieve the value of the 'id' property from that object.

2 Comments

@VivekKumarSrivastava what don't you understand?
@VivekKumarSrivastava Not specifically, it's for Java using the Mozilla Rhino library: mozilla.org/rhino. It will work on Android though.
0

Maybe you just need to use a JavaScript auto executing function like this:

(function(x, y){
  var result;
  result = x + y; // do some calculations
  return result;
})(1 , 2); //  you can set your parameters from Java

and 1, 2 are just two parameters from Java

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.