从JNI获取Java中使用的字符串
发布时间:2020-12-15 08:41:14 所属栏目:Java 来源:网络整理
导读:JAVA代码 以下是我在JAVA中编写的代码的一部分,您可以看到这是一个名为JC_VerificationCandidate的类,它有两个String成员enrollmentID和seedIndex. class JC_VerificationCandidate { public JCDSM_VerificationCandidate( String enrollmentID,String seedI
JAVA代码
以下是我在JAVA中编写的代码的一部分,您可以看到这是一个名为JC_VerificationCandidate的类,它有两个String成员enrollmentID和seedIndex. class JC_VerificationCandidate { public JCDSM_VerificationCandidate( String enrollmentID,String seedIndex ) { this.enrollmentID = enrollmentID; this.seedIndex = seedIndex; } public String enrollmentID; public String seedIndex; } 这里是主要类,我有本机方法,从那里我称之为本机方法. public class DsmLibraryTest extends Activity { @Override public void onCreate(Bundle savedInstanceState) { JCDSM_VerificationCandidate verificationCandidate[] = {new JCDSM_VerificationCandidate( "tom","anna" )}; dsm.JDSMVerify( 123456,"http:www.test_url.com",bytes,verificationCandidate ); } public native int JDSMVerify( int someValue1,String someValue2,byte[] someValue3,JC_VerificationCandidate jVerificationCandList[] ); } 正如您所看到的,我使用一个对象创建数组并将其提供给我的函数. JCDSM_VerificationCandidate verificationCandidate[] = {new JCDSM_VerificationCandidate( "tom","anna" )}; JNI代码 我怎样才能获得我从java应用程序设置的两个字符串enrollmentID,eedIndex以及哪些存储在jVerificationCandList中? JNIEXPORT jint JNICALL Java_com_Dsm_Test_DSM_JDSMVerify( JNIEnv* env,jobject thiz,jint jhDevice,jstring jurlID,jbyteArray jInputInfo,jobjectArray jVerificationCandList ) { } 解决方法
以下代码应允许您访问字段enrollmentID.使用
JNI String functions读取/操作它们.
// Load the class jclass jclass_JCV = env->FindClass(env,"my.package.JC_VerificationCandidate"); jfieldID fid_enrollmentID = env->GetFieldID(env,jclass_JCV,"enrollmentID","Ljava/lang/String;"); // Access the first element in the jVerificationCandList array jobject jc_v = env->GetObjectArrayElement(env,jVerificationCandList,0); // get reference to the string jstring jstr = env->GetObjectField(env,jc_v,enrollmentID); // Convert jstring to native string const char *nativeString = (*env)->GetStringUTFChars(env,jstr,0); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |