Get the string used in Java from JNI

java code

The following is a part of the code I wrote in Java. You can see that this is a code named JC_ Verificationcandidate class, which has two string members, enrollmentid and seedindex

class JC_VerificationCandidate {

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

    public String enrollmentID;
    public String seedIndex;
}

Here is the main class. I have native methods, which I call native methods from there

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[] );
}

As you can see, I use an object to create an array and provide it to my function

JCDSM_VerificationCandidate verificationCandidate[] = {new JCDSM_VerificationCandidate( "tom","anna" )};

JNI code

How can I get the two strings I set from the Java application, enrollmentid, eedindex, and which are stored in jverificationcandlist?

JNIEXPORT jint JNICALL Java_com_Dsm_Test_DSM_JDSMVerify( jnienv* env,jobject thiz,jint jhDevice,jstring jurlID,jbyteArray jInputInfo,jobjectArray jVerificationCandList ) {


}

Solution

The following code should allow you to access the field enrollmentid Use JNI string functions to read / manipulate them

// 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);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>