JNI。如何从jobject获取jstring并将其转换为char *

时间:2013-12-10 17:29:35

标签: java c java-native-interface char-pointer

这是我到目前为止所做的: 我传递一个有2个字段的对象:String和Integer作为参数 我想发送信息来处理它在C部分,这在这一点上并不重要...... 我在jstring声明中抱怨

JNIEXPORT jint JNICALL Java_Tier3_NativeMethods_totalPalletsIn(
            JNIEnv *env, jclass cls, jobject stat) {

jclass staticsitcs = (*env)->GetObjectClass(env, stat);

// Here I try to get it using the ID
jfieldID idDate = (*env)->GetFieldID(env, staticsitcs, "date", "S");

jstring dateString = (jstring)(*env)->GetStringRegion(env, stat, idDate);

// Here converting whatever I get in jstring to char*
char* date = (*env)->GetStringUTFChars(env,dateString,0);

// Getting the 2nd field from the object
jfieldID idNumber = (*env)->GetFieldID(env, staticsitcs, "amount", "I");

jint amount = (*env)->GetDoubleField(env, stat, idNumber);

// Calling C method
jint totalPallets = checkTotalPalletsIn(date, amount);

(*env)->ReleaseStringUTFChars(env, dateString, date);

return totalPallets;
}

我错过了什么?

1 个答案:

答案 0 :(得分:11)

jstring dateString = (jstring)(*env)->GetObjectField(env, stat, idDate);

......之后一切都还好。