我正在尝试在我的应用程序中集成一些 JNI 代码,这些代码从 Web 服务接收数据并从那里在 JNI 层中解析它我尝试通过传递从 Web 服务接收到的一些参数来调用 Java 层中定义的方法。以下是函数和方法的流程:
为 jclass 和 jmethodID 定义的变量:
static jclass userInfo = NULL ;
static jmethodID create_user_info = NULL ;
在 c 层定义的结构来保存值
struct User_Info {
long userId; // 1
std::string firstName; // 2
std::string lastName; // 3
bool follow; // 4
};
这个功能有问题。该函数在从 webservice 接收数据、解析并创建 struct User_Info 后调用
static jobject createJavaObject(JNIEnv *env, const User_Info& info) {
if(!userInfo){
userInfo = (jclass)env->NewGlobalRef(env->FindClass("com/model/UserInfo"));
assert(userInfo != NULL);
create_user_info = env->GetStaticMethodID(userInfo, "createUserInfo", "("
"I" // userId // 1
"Ljava/lang/String;" // firstName // 2
"Ljava/lang/String;" // lastName // 3
"Z" // follow // 4
")Lcom/model/UserInfo;");
assert(create_user_info != 0);
}
jstring firstName = cast<jstring>(env, info.first_name); // cast function just converts string to jstring
assert(firstName != NULL);
jstring lastName = cast<jstring>(env, info.last_name); // cast function just converts string to jstring
assert(lastName != NULL);
// ISSUE IS WITH FOLLOWING CALL
jobject res = env->CallStaticObjectMethod(userInfo, create_user_info,
info.userId, // 1
firstName, // 2
lastName, // 3
info.follow // 4
);
env->DeleteLocalRef(firstName);
env->DeleteLocalRef(lastName);
return res;
}
// cast function:
jstring cast<jstring, char const *>()(JNIEnv *env, char const *s) {
if (!s) return 0;
jbyteArray bytes = env->NewByteArray(strlen(s));
env->SetByteArrayRegion(bytes, 0, strlen(s), (jbyte*)s);
jmethodID midStringCtor = env->GetMethodID(clsString, "<init>", "([BLjava/lang/String;)V");
jstring ret = (jstring)env->NewObject(clsString, midStringCtor, bytes, encoding);
env->DeleteLocalRef(bytes);
return ret;
}
Java方法:
@Native
private static UserInfo createUserInfo(
int userId, // 1
String firstname, // 2
String lastname, // 3
boolean follow // 4
) {
UserInfo info = new UserInfo();
info.userId = userId;
info.firstname = firstname;
info.lastname = lastname;
info.follow = follow;
return info;
}
问题是当我运行代码时,我收到以下错误:
JNI 错误(应用程序错误):预期 jboolean (0/1) 但将值 1048605 作为 com.model.UserInfo com.model.UserInfo.createUserInfo(int, java.lang.String, java.lang.String,布尔值)
当我更改 java 方法和 jni 函数中的参数顺序并在 int 'userId' 之后保持布尔值 'follow' 时,不会发生问题。
我的问题是:
JNI 是否需要根据参数类型对参数进行排序,例如长/布尔之前的 int 参数,或对象之前的原始参数等。
如果是,是否有任何参考/文档建议它,如果没有,那么为什么我的代码在更改参数顺序后工作?