简体   繁体   中英

JNI. How to get jstring from jobject and convert it to char*

This is what I have so far: I pass an object which has 2 fields: String and Integer, as a parameter and I want to send information to process it in C part, which is not important at this point... I get complains at jstring declaration

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

What am I missing?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM