Get Strings used in Java from JNI(从 JNI 获取 Java 中使用的字符串)
问题描述
这是我用 JAVA 编写的代码的一部分,如您所见,这是一个名为 JC_VerificationCandidate
的类两个 String
成员 enrollmentID
和 seedIndex
.
Here is some part of my code that I have written in JAVA, As you can see this is a class called JC_VerificationCandidate
that have 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 main class where I have native method and from where I called that native method.
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 create array with one object and give it to my function.
JCDSM_VerificationCandidate verificationCandidate[] = {new JCDSM_VerificationCandidate( "tom", "anna" )};
JNI 代码
如何获取我从 java 应用程序设置并存储在 jVerificationCandList<中的两个字符串
enrollmentID
、eedIndex
/code> ?
JNIEXPORT jint JNICALL Java_com_Dsm_Test_DSM_JDSMVerify( JNIEnv* env, jobject thiz, jint jhDevice, jstring jurlID,
jbyteArray jInputInfo, jobjectArray jVerificationCandList ) {
}
推荐答案
以下代码应该允许您访问字段 enrollmentID.使用 JNI 字符串函数 读取/操作它们.
The following code should allow you to access the field enrollmentID. Use the 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 = (jstring) env->GetObjectField(env, jc_v, enrollmentID);
// Convert jstring to native string
const char *nativeString = env->GetStringUTFChars(env, jstr, 0);
这篇关于从 JNI 获取 Java 中使用的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 JNI 获取 Java 中使用的字符串
基础教程推荐
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 在螺旋中写一个字符串 2022-01-01