Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem with call static methods in plugin

Discussion in 'Android' started by leekinon, Oct 20, 2015.

  1. leekinon

    leekinon

    Joined:
    Dec 4, 2013
    Posts:
    21
    How to call C# to java?

    I find some method in website but it not work.

    Here is my c# script

    Code (CSharp):
    1.     var activityJavaClass=new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    2. if(activityJavaClass!=null){
    3. Debug.Log("UnityPlayer class not null");
    4.                             // To get the activity
    5. var currentActivityJavaObject=activityJavaClass.GetStatic<AndroidJavaObject>("currentActivity");
    6. AndroidJavaObject app = currentActivityJavaObject.Call<AndroidJavaObject>("getApplicationContext");
    7.  
    8. if(currentActivityJavaObject!=null){
    9. Debug.Log("currentActivity object of UnityPlayer not null");
    10. var manager=GetComponent<CapturedScreenManager>()as CapturedScreenManager;
    11.  
    12. if(manager!=null){
    13.                                     //string path="/storage/emulated/0/DCIM/Camera/VID_20151017_072913.mp4";
    14. string path=manager.androidSavePath;
    15. if(path==null|| string.Equals(path,"")){
    16. Debug.LogError("Android save path is null or empty");
    17. return;
    18. }
    19.                                     Debug.Log("About to share to Youku from path: " + path);
    20.                                     // To get the share video class
    21.                                     AndroidJavaClass javaClass=new AndroidJavaClass("com.youku.uploader.ShareToYouku");
    22.  
    23. if(javaClass!=null){
    24. Debug.Log ("ShareToYouku java class not null");
    25. currentActivityJavaObject.Call("runOnUiThread", new AndroidJavaRunnable(() =>{
    26. javaClass.CallStatic("GetDataFromUnity", new object[]{ "32e7028d0a42009c","50cc4b8cfe70fd56ccdfecd7460e88b5",accessToken,title,tag, path, app});
    27.                                         }));
    28.                                     }else{
    29.                                         Debug.LogError("ShareToYouku java class is null");
    30.                                     }
    31.                                 }else{
    32.                                     Debug.LogError("Captured screen manager is null");
    33.                                 }
    34.                             }else {
    35.                                 Debug.LogError("currentActivity object of UnityPlayer is null");
    36.                             }
    37.                         }else{
    38.                             Debug.LogError("UnityPlayer class is null");
    39.                         }
    40.  
    Here is my java script

    Code (CSharp):
    1.     public static void GetDataFromUnity(String clientID,String client_secret,String access_token, String title, String tags, String file_name, Context context)
    2.  
    I already add jar into Plugin>Android. But it show error after the Debug Log (ShareToYouku java class not null)

    Code (CSharp):
    1.  
    2. 11-10 17:31:44.506: I/Unity(13572): AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/loopj/android/http/AsyncHttpClient;
    3. 11-10 12:05:45.585: I/Unity(20610):   at UnityEngine._AndroidJNIHelper.GetSignature (System.Object obj) [0x00000] in <filename unknown>:0
    4. 11-10 12:06:48.950: I/Unity(20610):   at UnityEngine._AndroidJNIHelper.GetSignature (System.Object[] args) [0x00000] in <filename unknown>:0
    5. 11-10 12:06:48.950: I/Unity(20610):   at UnityEngine._AndroidJNIHelper.GetMethodID (IntPtr jclass, System.String methodName, System.Object[] args, Boolean isStatic) [0x00000] in <filename unknown>:0
    6. 11-10 12:06:48.950: I/Unity(20610):   at UnityEngine.AndroidJNIHelper.GetMethodID (IntPtr jclass, System.String methodName, System.Object[] args, Boolean isStatic) [0x00000] in <filename unknown>:0
    7. 11-10 12:07:56.735: I/Unity(20610):   at UnityEngine.AndroidJavaObject._CallStatic (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0
    8. 11-10 12:07:56.735: I/Unity(20610):   at UnityEngine.AndroidJavaObject.CallStatic (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0
    9. 11-10 12:07:56.735: I/Unity(20610):   at UIButtonShare+<GetAccessToken>c__AnonStorey39+<GetAccessToken>c__AnonStorey36.<>m__60 () [0x00000] in <f
     
    Last edited: Oct 20, 2015
  2. leekinon

    leekinon

    Joined:
    Dec 4, 2013
    Posts:
    21
  3. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,559
    You didn't show the right code (for the GetACcessToken method). That is the one that is failing ...