从JNI获得Java中使用的字符串

问题描述

| 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应用程序中设置并存储在
jVerificationCandList
中的两个字符串
enrollmentID
eedindex
JNIEXPORT jint JNICALL Java_com_Dsm_Test_DSM_JDSMVerify( jnienv* env,jobject thiz,jint jhDevice,jstring jurlID,jbyteArray jinputInfo,jobjectArray jVerificationCandList ) {


}
    

解决方法

        以下代码应允许您访问字段enrollmentID。使用JNI String函数来读取/操作它们。
// 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 = (jstring) env->GetObjectField(env,jc_v,enrollmentID);

// Convert jstring to native string
const char *nativeString = env->GetStringUTFChars(env,jstr,0);