简体   繁体   English

从Java发送到JNI的参数未发送

[英]Parameters send from Java to JNI are not being sent

I have read about on how parameters are being send from Java to a JNI function. 我已经阅读了有关如何将参数从Java发送到JNI函数的信息。 I first tried to send an ArrayList because im trying to get Android phonebooks. 我第一次尝试发送ArrayList,因为我试图获取Android电话簿。

After a lot of problems I made it simple to be completely sure that a simple parameter (a String) is being send. 经过很多问题后,我变得很简单,可以完全确定要发送的是简单参数(字符串)。

This is my Java declaration of the function: 这是我对该函数的Java声明:

public static native void nativeCallback(String params);

and this is the JNI function: 这是JNI函数:

JNIEXPORT void JNICALL
Java_example_nativeCallback(JNIEnv*  env,jstring params)

I check and the function is being called. 我检查并正在调用该函数。 I used this to see if there is actually a String being send: 我用它来查看是否实际上正在发送一个字符串:

int sizeOfChain = env->GetStringLenght(params);
std::ostringstream convert;
convert<<sizeOfChain;
convert.flush();
std::string finalString = convert.str();

When printing finalString it is 0. 打印finalString时为0。

This is how the function is being called from Java: 这是从Java调用函数的方式:

nativeCallback("TEST_STRING");

Also, sending an ArrayList is valid as jobjectArray? 另外,发送ArrayList作为jobjectArray有效吗?

Try doing this to get a C string from the jstring 尝试执行此操作以从jstring获取C字符串

const char *str= env->GetStringUTFChars(params, NULL);

printf("String: %s\n", str);

env->ReleaseStringUTFChars(params, str);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM