How to handle an Android (java) string array?

  • I'm trying to use the Qt Android interface, #include <QAndroidJniObject>

    I've got the basic calls working. For instance I can create a java string with
    @QAndroidJniObject mixedCaseString = QAndroidJniObject::fromString("wHiCHcaSe");@
    and I can manipulate it, for instance
    @QAndroidJniObject lowercaseString = casedString.callObjectMethod("toLowerCase", "()Ljava/lang/String;");@
    I can confirm these are both working by converting to QStrings with mixedCaseString.toString() and lowercaseString.toString().

    I can apparently split the first string using "H" as a separator, with
    @ QAndroidJniObject splitter = QAndroidJniObject::fromString("H");
    QAndroidJniObject mixedcaseStringSplit= mixedCaseString.callObjectMethod("split", "(Ljava/lang/String;)[Ljava/lang/String;",splitter.object<jstring>());@

    The array of strings in mixedcaseStringSplit appears to be valid, and mixedcaseStringSplit.toString() returns the value (reasonable in the context, not NULL)

    But here I get stuck. How can I manipulate the elements in that array?

    Even attempts just to extract the size of the array seems to fail, e.g.
    @QAndroidJniObject mixedcaseStringSplitN = mixedcaseStringSplit.callObjectMethod("size", "()I");@
    returns NULL (more precisely, mixedcaseStringSplitN.isValid() is false)
    @jint mixedcaseStringSplitNf = mixedcaseStringSplit.getField<jint>("length");@
    returns 0. From that string, the size of the split array should be 3 (["w","iC","caSe"]).

    Does anyone have a better idea on java (Android) string array handling?

    I'm using Qt 5.2.1 from Digia's online package.

  • Hmm, scrambling my java and C++. Java arrays have no size() method, so mixedcaseStringSplit.callObjectMethod("size", "()I") ought not to work.

    But the length of a java array is given by the length property, so mixedcaseStringSplit.getField<jint>("length") ought to work. In Qt we access java properties with getField, right?

    And once we do understand how to access java_array.length, how do we extract the elements? Java has no i ) function, only java_array[i]. Certainly, callObjectMethod("[]",...) does not work.

  • Seems that Qt just can't do it, not directly. Have to go the long round instead, using "JNI": explicitly (via the "JNI array operations":

    @#include <QAndroidJniEnvironment>
    QAndroidJniEnvironment jniEnv;@


    @int splitArrayLength = jniEnv->GetArrayLength(mixedcaseStringSplit.object<jarray>());@
    gives the length of the array and
    @ QAndroidJniObject arrayElement = jniEnv->GetObjectArrayElement(mixedcaseStringSplit.object<jobjectArray>(), i );@
    gives a specific element in the array.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.