mafioso
mafioso

Reputation: 1632

How to get string value of jobject in C using JNI?

My jobject looks like that {"A", "B", 1} and now I want to get the String value of that.

Currently I have that code:

class clazz = (*env)->FindClass(env, "model/Spieler");
jmethodID midVorname = (*env)->GetMethodID(env, clazz, "getVorname", "()Ljava/lang/String;");
jmethodID midNachname = (*env)->GetMethodID(env, clazz, "getNachname", "()Ljava/lang/String;");
jmethodID midTrikotnummer = (*env)->GetMethodID(env, clazz, "getTrikotnummer", "()I");

char vorname[SIZE];
char nachname[SIZE];
int trikotnummer;
jobject newObj;

link = (Spieler*) malloc(sizeof(Spieler));

newObj = (*env)->GetObjectArrayElement(env, arr, i);

trikotnummer = (*env)->CallIntMethod(env, newObj, midTrikotnummer);

For Integer it works like above but I have no clue how to get the String values by the jmethodID.
Do you have any suggestions?

Upvotes: 1

Views: 1380

Answers (1)

Mr.C64
Mr.C64

Reputation: 42924

You may try something like this:

/* Access the i-th element in the array */
jobject obj  = (*env)->GetObjectArrayElement(env, arr, i);

/* Call the method */
jobject resultString = (*env)->CallObjectMethod(env, obj, /* method ID */ midVorname);

/* Get a C-style string */
const char* str = (*env)->GetStringUTFChars(env, (jstring) resultString, NULL);

/* Use the string ... */

/* Clean up */
(*env)->ReleaseStringUTFChars(env, resultString, str);

Upvotes: 1

Related Questions