获取JNI中Java中使用的字符串

 2023-01-20    300  

问题描述

Java代码

这是我用 java 编写的代码的某个部分enrollmentID和seedIndex.

class JC_VerificationCandidate {

    public JCDSM_VerificationCandidate( String enrollmentID, String seedIndex ) {
        this.enrollmentID = enrollmentID;
        this.seedIndex    = seedIndex;
    }

    public String enrollmentID;
    public String seedIndex;
}

这是我有本机方法的主要类,从我称之为本机方法的地方.

获取JNI中Java中使用的字符串

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 ) {


}

推荐答案

以下代码应允许您访问字段注册.使用 >阅读/操纵它们.

// 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);

以上所述是小编给大家介绍的获取JNI中Java中使用的字符串,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/26062.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。