Explanation of and examples for customCodeService.RunJavaCode seem to be wrong.

0 votes

 

The API and examples for  customCodeService.RunJavaCode shows that it needs three arguments:
 
RunJavaCode(name, jsonBody, new UnityCallBack());
 
But when I try to use it I get an error: 
 
error CS1502: The best overloaded method match for `com.shephertz.app42.paas.sdk.csharp.customcode.CustomCodeService.RunJavaCode(string, SimpleJSON.JSONClass, com.shephertz.app42.paas.sdk.csharp.App42CallBack)' has some invalid arguments
 
But the autocomplete in MonoDevelop shows that it takes two arguments only: 
 
customCodeService.RunJavaCode(name, jsonBody);
 
This gives no errors but then I can't specify a callback.
 

 

closed with the note: Closing this thread as there was no update from last 2 Weeks or more. Please raise a new query if problem persists.
asked Jul 27, 2014 in App42 Cloud API-BaaS by huffman (17 points)
closed Sep 22, 2014 by sushil
Then when I run it I get this error:

App42SecurityException: {"app42Fault":{"httpErrorCode":401,"appErrorCode":1401,"message":"UnAuthorized Access","details":"Client is not authorized"}}
com.shephertz.app42.paas.sdk.csharp.connection.RESTConnectorAsync.ExecuteCustomCode (System.String url, System.Collections.Generic.Dictionary`2 paramsDics, System.String bodyPayLoad, System.Collections.Generic.Dictionary`2 headerParams)
com.shephertz.app42.paas.sdk.csharp.customcode.CustomCodeService.RunJavaCode (System.String name, SimpleJSON.JObject jsonBody)
There are mainly 2 reasons of UnAuthorized Access (1401) Exception
-          Either your API Key & Secret Key is not correct
-          Or the device time is not correct on which app is running.
Could you please make sure that API Key and Secret Key are correct and also the time of the device is correct on which App is running and can you tell me that in which sdk version you are working on?
Thanks.
I know the keys are ok because i just copied them from other parts of my code that is working.
The time on my iMac is accurate. The SDK version is 2.8 (App42_Unity3D_SDK_2.8.dll)
About the error (error CS1502: The best overloaded method match for) you are getting, Please make sure that your UnityCallBack reference class implements App42CallBack. Because I have checked in the code and its working fine at our end. This problem comes when reference to callback class is missing.
 
Custom code service have two overloaded methods in which one method takes 2 arguments and the other method takes 3 arguments.
 
Please confirm and let me know if it solves your problem.
Thanks.
I still get this error:
"The best overloaded method match for `com.shephertz.app42.paas.sdk.csharp.customcode.CustomCodeService.RunJavaCode(string, SimpleJSON.JSONClass, com.shephertz.app42.paas.sdk.csharp.App42CallBack)' has some invalid arguments"
// in the body of my code

void OnEnable()
{
     String name = "MyCustomCode";
     JObject jsonBody = new JObject();  
     jsonBody.Add("Company", "Shephertz");  
     App42Log.SetDebug(true);                //Print output in your editor console
     CustomCodeService customCodeService = App42API.BuildCustomCodeService();

     customCodeService.RunJavaCode(name, jsonBody, new verifyAppleReceiptResponse());
}

// callback
public class verifyAppleReceiptResponse : App42CallBack
{  
    public void OnSuccess(object response)   
    {  
        JObject res= (JObject)response;   
        Debug.Log("verifyAppleReceiptResponseresponse: "+res["name"]) ;  
    }
   
    public void OnException(Exception e)   
    {  
        App42Exception exception = (App42Exception)e;
        int appErrorCode = exception.GetAppErrorCode();
        string fetchedJSONObject = exception.GetMessage();
        //JObject jsonObj = JObject.Parse(fetchedJSONObject);
       
        Debug.Log("verifyAppleReceiptResponse Exception: "+ fetchedJSONObject);  
    }  
}

NOTE: I'm also getting this error:

"error CS1503: Argument `#2' cannot convert `SimpleJSON.JObject' expression to type `SimpleJSON.JSONClass'"


I got at the top of the C# script.

using SimpleJSON;
Can you please replace the below code snippet:

from:

 JObject jsonBody = new JObject();  
 jsonBody.Add("Company", "Shephertz");
 
To:
 JSONClass jsonBody = new JSONClass();  
 jsonBody.Add("Company", "Shephertz");  

Let me know if it solve your problem.
That's the way I had it at first and that gave me errors (probably for another reason). But I put it back to the way you just suggested and now it works. Thanks!

But now when I run the code I am getting this error:


verifyAppleReceiptResponse Exception: {"httpErrorCode":"400", "appErrorCode":"1400", "message":"Bad Request", "details":"The Request parameters are invalid."}

///////////////////////////////////////////////////////////
//calling routine:
JSONClass jsonBody = new JSONClass();
       
        jsonBody.Add("Company", "Shephertz");  
       
        App42Log.SetDebug(true);                //Print output in your editor console
       
        CustomCodeService customCodeService = App42API.BuildCustomCodeService();
        customCodeService.RunJavaCode("MyCustomCode", jsonBody, new verifyAppleReceiptResponse());

///////////////////////////////////////////////////////////
// Callback routine:
public class verifyAppleReceiptResponse : App42CallBack
{  
    public void OnSuccess(object response)   
    {  
        JObject res= (JObject)response;   
        Debug.Log("verifyAppleReceiptResponseresponse: "+res["name"]) ;  
    }
   
    public void OnException(Exception e)   
    {  
        App42Exception exception = (App42Exception)e;
        int appErrorCode = exception.GetAppErrorCode();
        string fetchedJSONObject = exception.GetMessage();
        //JObject jsonObj = JObject.Parse(fetchedJSONObject);
       
        Debug.Log("verifyAppleReceiptResponse Exception: "+ fetchedJSONObject);  
    }  
}

///////////////////////////////////////////////////////////
// the Java custom code that I uploaded to your server:

package com.shephertz.app42.paas.customcode.sample;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import com.shephertz.app42.paas.customcode.Executor;
import com.shephertz.app42.paas.customcode.HttpRequestObject;
import com.shephertz.app42.paas.customcode.HttpResponseObject;
import com.shephertz.app42.paas.sdk.java.ServiceAPI;
import com.shephertz.app42.paas.sdk.java.log.LogService;


public class MyCustomCode implements Executor {

    private ServiceAPI sp = new ServiceAPI("*** HIDDEN ***","*** HIDDEN ***");
    private final int HTTP_STATUS_SUCCESS = 200;
    private String moduleName = "App42CustomCodeTest";
   
    /**
     * Write your custom code inside this method
     */
   
    @Override
    public HttpResponseObject execute(HttpRequestObject request)
    {
        JSONObject body = request.getBody();

        // Build Log Service For logging in Your Code
        LogService logger = sp.buildLogService();
        logger.debug("Recieved Request Body: " + body.toString(), moduleName);

        // Write Your Custom Code Here
        // ......//

        logger.info("Running Custom Code: ", moduleName);
       
        // Create JSON Response Based on Your business logic
        JSONObject jsonResponse = new JSONObject();
       
        try
        {
            jsonResponse.put("status", "success");  
            jsonResponse.put("message", "From Custom Code");  
            //....//
            //....//
        }
        catch (JSONException e)
        {
            // Do exception Handling for JSON Parsing
        }
       
        // Return JSON Response and Status Code
        return new HttpResponseObject(HTTP_STATUS_SUCCESS, jsonResponse);
    }

}

///////////////////////////////////////////////////////////
NOTE:  Why am I not seeing any output in the 'Technical Server Manager' Logging Service window of the App42 Management Console. console?
///////////////////////////////////////////////////////////
BTW. This forum needs a bigger comment window and a way to specify what part of the comment is code.
Hi Huffman,
You just have to initialize App42API before your code snippet like :-
App42API.Initialize("*** HIDDEN ***","*** HIDDEN ***");
JSONClass jsonBody = new JSONClass();
jsonBody.Add("Company", "Shephertz");  
App42Log.SetDebug(true);                //Print output in your editor console
CustomCodeService customCodeService = App42API.BuildCustomCodeService();
customCodeService.RunJavaCode("MyCustomCode", jsonBody, new verifyAppleReceiptResponse());

Due to bad request exception your custom code hasn't been processed i.e.you are not able to get logs on your App HQ management  Console.
Thanks. If for my game room code I use:
    WarpClient.initialize(apiKey,secretKey);
for my user, storage, & custom code do I also have to use:
       App42API.Initialize(apiKey, secretKey);

In other words, are both necessary?

NOTE: For my user and storage I never used App42API.Initialize and everything still worked. This was used instead:

      ServiceAPI sp = new ServiceAPI(apiKey, secretKey);

So is there a difference between the design of your API user or storage calls, and calls for using custom code?
I added App42API.Initialize("*** HIDDEN ***","*** HIDDEN ***");  to my code snippet. But now i'm getting these errors:

https://www.evernote.com/shard/s3/sh/2be937d0-fa10-4f2f-a479-bbee918bc07c/169cd4c6b1499103f1df14d686323303

Then after about 30 seconds to a minute my callback reports this:

"Exception: com.shephertz.app42.paas.sdk.csharp.App42Exception: 504 Gateway Timeout"
In Server side Custom code, use ServiceAPI reference to initialize. However in your client code, you can use either App42API or serviceAPI. If possible send me your app name along with name of your custom code service at ajay.tiwari@shephertz.com.
I have to check from the backend about your issue to solve it quickly.
Let me know if it helps.

Thanks
Ajay
Ajay. Thanks for your email. My build.xml file was already set to use Java 1.6 but I had only 1.8 on my Mac and had Eclipse setup to use 1.8 with compatibility set to 1.6 which was causing the problems you could see. So I downloaded and installed JAVA SE6 (JDK 1.6) from Apple, configured Eclipse accordingly and now everything works. Thanks.

But there is one simple problem: MyCustomCode.java is not able to output the value of the private string "moduleName" with logger.debug or logger.info. I tried moving the definition of "moduleName" to the execute function but that didn't help. This is not important and I don't know Java, but maybe something is wrong with that example code:

------------------------------------
public class MyCustomCode implements Executor {

    private ServiceAPI sp = new ServiceAPI("cee4698f3697d7e61c05dcd744d06927f26027444f29ae14bb9f03a08a968f21","b63e166fa826160e5959217a632a96906602307b22553cc4ee71826e87b0a838");
    private final int HTTP_STATUS_SUCCESS = 200;
    //private String moduleName = "App42CustomCodeTest";
   
    /**
     * Write your custom code inside this method
     */
   
    @Override
    public HttpResponseObject execute(HttpRequestObject request)
    {
        JSONObject body = request.getBody();

        String moduleName = "App42 TOT CustomCodeTest";

        // Build Log Service For logging in Your Code
        LogService logger = sp.buildLogService();
        logger.debug("Recieved Request Body: " + body.toString(), moduleName);

        // Write Your Custom Code Here
        // ......//

        logger.info("Running Custom Code: ", moduleName);
       
        // Create JSON Response Based on Your business logic
        JSONObject jsonResponse = new JSONObject();
       
        try
        {
            jsonResponse.put("status", "success");  
            jsonResponse.put("message", "From TOT Custom Code");  
            //....//
            //....//
        }
        catch (JSONException e)
        {
            // Do exception Handling for JSON Parsing
        }
       
        // Return JSON Response and Status Code
        return new HttpResponseObject(HTTP_STATUS_SUCCESS, jsonResponse);
    }
Your code looks fine. You should be able to see it in AppHQ console under Technical Service > Log Service tab.

Please check and confirm.

Thanks

Ajay
That's the problem. I can't see it there. the string is not output.
Just looked at your log and can see your app module name there. It may be a problem in AppHQ UI seems. Can you send me the screen shot for the same?
I can't see it. Seems like there is a problem with the info logging. It should be pull the module name from the variable and display it after "Running Custom Code: " which is a string literal.

https://www.evernote.com/shard/s3/sh/12dda2f8-6155-4f6d-9d38-370d938d9120/58585729cc253df73f45f35a7d9f9abf
Module name does not get printed with the message. There is a separate column for module name (First column in the table) which contains this value.

Let me know if it helps. Also, requesting you to please close this question if this query gets resolved.

Thanks

Ajay
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...