1

Considering this Java class with the static method:

public class TestClass{
    public string str;
    public TestClass() {
        str = "Test From Java";
    }
    public static String staticMethod() {
        return "Test From Java";
    }
}

I have written these lines of code in c++ file:

QAndroidJniObject str =  QAndroidJniObject::callStaticObjectMethod(
                                   "org/.../TestClass"
                                   ,"staticMethod"
                                   ,"(V)Ljava/lang/String;");

It seems everything is working but I don't know how can I use the str Object. I tried to convert it to a QString object using str.tostring() method but it always returns an empty string. Why it does not work as expected? I've also tested ()Ljava/lang/String; for method signature but no success!
Thanks in advance.

Nejat
  • 31,784
  • 12
  • 106
  • 138
a.toraby
  • 3,232
  • 5
  • 41
  • 73

1 Answers1

1

You should specify the returned JNI type in <...> when calling the method :

QAndroidJniObject str =  QAndroidJniObject::callStaticObjectMethod<jstring>(
                               "org/.../TestClass"
                               ,"staticMethod");

QString string = str.toString();

Here there is no need to define the signature since your function has no argument.

Nejat
  • 31,784
  • 12
  • 106
  • 138
  • Thank you Nejat. It worked successfully. But what about a static method which return type is string and has some input parameters? I tried use callStaticObjectMethod but there is no matching function: `QAndroidJniObject::callStaticObjectMethod("org/.../TestClass" ,"staticMethod" ,"(Ljava/lang/String;)Ljava/lang/String;" ,val.object());` – a.toraby Feb 24 '15 at 11:08
  • val is a QAndroidJniObject: `QAndroidJniObject::fromString("This is test");` – a.toraby Feb 24 '15 at 11:09
  • Nothing seems wrong with your approach, i can't think of anything. May be it's better to ask this problem in a new question and describe it more. – Nejat Feb 24 '15 at 11:48