JNI将unsigned char *转换为jbyteArray并将jbyteArray返回给java

时间:2016-03-23 15:37:19

标签: java android c android-ndk java-native-interface

我使用c得到了一个未签名的char *数据,这是由AES进行的,我希望将这些数据转换为jbyteArryay并将其返回给java: 这是代码:

{
   unsigned char* token = get_token();//get token from AES decrypt method

   LOGV("token is:%s",token);//it shows me that string like:"jghgillldjdmdejkgmmjdkf"

   /**
    * when i try this line:unsigned char* token = "jghgillldjdmdejkgmmjdkf";i works.
    */

   jclass strClass = (*env)->FindClass(env,"java/lang/String");
   jmethodID ctorID = (*env)->GetMethodID(env,strClass,"<init>", "([BLjava/lang/String;)V");
   jbyteArray bytes = (*env)->NewByteArray(env,strlen(token));
   (*env)->SetByteArrayRegion(env,bytes,0,strlen(token),(jbyte*)token);
   return bytes;
}

但是当我试图从java获得价值时,我什么都没有,这里有谁可以帮助我?我真的遇到了麻烦,非常感谢你!

更新 我尝试了另一种方式:

{
char *token = get_token();
return (*env)->NewStringUTF(env,token);
}

但是android设备崩溃了,错误日志是:

03-23 13:15:01.951: A/art(5211): art/runtime/check_jni.cc:65] JNI DETECTED   ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal continuation byte 0x2c
03-23 13:15:01.951: A/art(5211): art/runtime/check_jni.cc:65]     string: 'igkjiekefkdiijikjljmdlf?,x??'
03-23 13:15:01.951: A/art(5211): art/runtime/check_jni.cc:65]     in call to NewStringUTF
03-23 13:15:01.951: A/art(5211): art/runtime/check_jni.cc:65]     from java.lang.String com.asmine.ssonative.SSONative.getToken(java.lang.String, java.lang.String)
03-23 13:15:01.951: A/art(5211): art/runtime/check_jni.cc:65] "Thread-313" prio=5 tid=15 Runnable
03-23 13:15:01.951: A/art(5211): art/runtime/check_jni.cc:65]   | group="main" sCount=0 dsCount=0 obj=0x12ce50a0 self=0xed22a800
03-23 13:15:01.951: A/art(5211): art/runtime/check_jni.cc:65]   | sysTid=5232 nice=0 cgrp=apps sched=0/0 handle=0xf435ab80
03-23 13:15:01.951: A/art(5211): art/runtime/check_jni.cc:65]   | state=R schedstat=( 859393537 12390828 74 ) utm=83 stm=2 core=3 HZ=100
03-23 13:15:01.951: A/art(5211): art/runtime/check_jni.cc:65]   | stack=0xe25fe000-0xe2600000 stackSize=1036KB
03-23 13:15:01.951: A/art(5211): art/runtime/check_jni.cc:65]   | held mutexes= "mutator lock"(shared held)
03-23 13:15:01.951: A/art(5211): art/runtime/check_jni.cc:65]   native: #00 pc 00005ac3  /system/lib/libbacktrace_libc++.so (UnwindCurrent::Unwind(unsigned int, ucontext*)+83)
03-23 13:15:01.951: A/art(5211): art/runtime/check_jni.cc:65]   native: #01 pc 00002e91  /system/lib/libbacktrace_libc++.so (Backtrace::Unwind(unsigned int, ucontext*)+33)
03-23 13:15:01.951: A/art(5211): art/runtime/check_jni.cc:65]   native: #02 pc 003c3f0a  /system/lib/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, int, char const*, art::mirror::ArtMethod*)+138)
03-23 13:15:01.951: A/art(5211): art/runtime/check_jni.cc:65]   native: #03 pc 0038e85c  /system/lib/libart.so (art::Thread::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const+284)
03-23 13:15:01.951: A/art(5211): art/runtime/check_jni.cc:65]   native: #04 pc 0010131b  /system/lib/libart.so (art::JniAbort(char const*, char const*)+1019)
03-23 13:15:01.951: A/art(5211): art/runtime/check_jni.cc:65]   native: #05 pc 00101ecc  /system/lib/libart.so (art::JniAbortF(char const*, char const*, ...)+108)
03-23 13:15:01.951: A/art(5211): art/runtime/check_jni.cc:65]   native: #06 pc 00106b36  /system/lib/libart.so (art::ScopedCheck::Check(bool, char const*, ...) (.constprop.130)+3046)
03-23 13:15:01.951: A/art(5211): art/runtime/check_jni.cc:65]   native: #07 pc 0010cd4d  /system/lib/libart.so (art::CheckJNI::NewStringUTF(_JNIEnv*, char const*)+93)

0 个答案:

没有答案