android NDK - passing 2 strings from java to C code in JNI

1,713 views
Skip to first unread message

Guglielmo Buzzulini

unread,
Aug 29, 2013, 11:15:53 AM8/29/13
to andro...@googlegroups.com
hello

i need to pass 2 strings from android activity to C native code in JNI folder 

from android activity i use :

String uno = "prima";
String due = "prova";
String a = stringFromJNI(uno, due);


from C code in JNI folder i have this function :

#include <string.h>
#include <jni.h>

/* This is a trivial JNI example where we use a native method
 * to return a new VM String. See the corresponding Java source
 * file located at:
 *
 *   apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java
*/

jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz, jstring a, jstring b )
{
char s[100];

const char *aa = (*env)->GetStringUTFChars(env, a, 0);
const char *bb = (*env)->GetStringUTFChars(env, b, 0);

strcat(s, aa);
strcat(s, bb);

        return (*env)->NewStringUTF(env, s);
}


compile function is ok , 
the execution give me this message :

08-29 16:57:10.229: W/dalvikvm(13846): JNI WARNING: input is not valid Modified UTF-8: illegal continuation byte 0x70
08-29 16:57:10.229: W/dalvikvm(13846): string: ',#�primaprova'
08-29 16:57:10.229: W/dalvikvm(13846): in 
com/example/hellojni/HelloJni;.stringFromJNI:(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; (NewStringUTF)
08-29 16:57:10.229: I/dalvikvm(13846): "main" prio=5 tid=1 NATIVE
08-29 16:57:10.229: I/dalvikvm(13846): | group="main" sCount=0 dsCount=0 obj=0x409e8460 self=0x12800
08-29 16:57:10.229: I/dalvikvm(13846): | sysTid=13846 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=1075102856
08-29 16:57:10.229: I/dalvikvm(13846): | schedstat=( 308264887 107238356 282 ) utm=29 stm=1 core=0


why ?

tanks for help


william


Bakcsi Zsolt

unread,
Sep 11, 2013, 10:50:08 AM9/11/13
to andro...@googlegroups.com
Hi,

I think initialization of char s[100] is missing:
char s[100];
s[0] = 0;

Probably not related to your current problem, but you should free aa and bb:
(*env)->ReleaseStringUTFChars(env, a, aa);

Regards,
Zsolt
> --
> You received this message because you are subscribed to the Google Groups "android-ndk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
> To post to this group, send email to andro...@googlegroups.com.
> Visit this group at http://groups.google.com/group/android-ndk.
> For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages