本文介绍了JNI.如何从jobject获取jstring并将其转换为char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我到目前为止所拥有的:我传递了一个具有2个字段的对象:String和Integer,作为参数我想发送信息以C部分的形式进行处理,这在当时并不重要...我在jstring声明中抱怨
This is what I have so far:I pass an object which has 2 fields: String and Integer, as a parameterand 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;
}
我想念什么?
推荐答案
jstring dateString = (jstring)(*env)->GetObjectField(env, stat, idDate);
…然后一切正常.
这篇关于JNI.如何从jobject获取jstring并将其转换为char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!