简体   繁体   中英

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. I first tried to send an ArrayList because im trying to get Android phonebooks.

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:

public static native void nativeCallback(String params);

and this is the JNI function:

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.

This is how the function is being called from Java:

nativeCallback("TEST_STRING");

Also, sending an ArrayList is valid as jobjectArray?

Try doing this to get a C string from the jstring

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

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

env->ReleaseStringUTFChars(params, str);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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