如何使用JNI在C中获取jobject的字符串值?

时间:2016-10-13 08:55:34

标签: c object java-native-interface

我的jobject看起来像{"A", "B", 1},现在我想得到它的String值。

目前我有这段代码:

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);

对于Integer,它的作用与上面相同,但我不知道如何通过jmethodID获取String值。
你有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

/* 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);