Android JNI C简单附加功能 [英] Android JNI C simple append function

查看:73
本文介绍了Android JNI C简单附加功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Id喜欢做一个简单的函数,该函数返回两个String的值.

Id like to make a simple function, that return value of two Strings.

基本上:

java

public native String getAppendedString(String name);

c

jstring Java_com_example_hellojni_HelloJni_getAppendedString(JNIEnv* env,   jobject thiz, jstring s) {
    jstring sx = (*env)->GetStringUTFChars(env, s, NULL);

    return ((*env)->NewStringUTF(env, "asd ")+sx);
}

jni/hello-jni.c:32:警告:初始化会从指针目标类型中丢弃限定符 jni/hello-jni.c:34:错误:对二进制+无效的操作数(具有"char *"和"char *")

jni/hello-jni.c:32: warning: initialization discards qualifiers from pointer target type jni/hello-jni.c:34: error: invalid operands to binary + (have 'char *' and 'char *')

检索将是:"asd qwer",我该怎么办?

the retval will be: "asd qwer", how can I do this?

编辑

jstring s1  = (*env)->NewStringUTF(env, "456");
jstring s2  = (*env)->NewStringUTF(env, "123");
jstring sall=strcat(s1, s2);

return sall;

仅返回"456"

编辑了#3 THE LAST

edited #3 THE LAST

最终的工作代码为:

jstring Java_com_example_hellojni_HelloJni_getAppendedString(JNIEnv* env,   jobject thiz, jstring s1) {
    D("HMMMMMMMMMMMMMMMMMMM");
    jstring s2  = (*env)->NewStringUTF(env, "123");

    jbyte *s1x  = (*env)->GetStringUTFChars(env, s1, NULL);
    jbyte *s2x  = (*env)->GetStringUTFChars(env, s2, NULL);

    char *sall  = malloc(strlen(s1x) + strlen(s2x) + 1);
    strcpy(sall, s1x);
    strcat(sall, s2x);

    jstring retval = (*env)->NewStringUTF(env, sall);

    (*env)->ReleaseStringUTFChars(env, s1, s1x);
    (*env)->ReleaseStringUTFChars(env, s2, s2x);
    free(sall);

    return retval;
}

谢谢莱斯利

推荐答案

这里有一些问题:

  1. GetStringUTFChars返回jbyte *(以N结尾的C字符串),而不是jstring.您需要使用此C字符串在C中进行字符串操作.

  1. GetStringUTFChars returns a jbyte * (a null-terminated C string), not a jstring. You need this C string to do string manipulation in C.

完成后,您需要致电ReleaseStringUTFChars.

您需要使用malloc分配足够的内存来容纳串联的字符串.

You need to allocate enough memory to hold the concatenated string, using malloc.

如前所述,您需要用strcat连接两个C字符串. (您不能使用+运算符来执行此操作.+应用于指针时,将从原始指针的偏移量返回指针.)

As ethan mentioned, you need to concatenate your two C strings with strcat. (You cannot do this with the + operator. When applied to a pointer, + returns the pointer from the offset of the original pointer.)

请记住在完成分配后(即,将其作为Java字符串插入之后)释放分配的内存.

Remember to free the memory you allocated after you're done with it (ie, after it's been interned as a Java string.)

您应该执行以下操作:

char *concatenated;
const jbyte *sx;
jstring retval;

/* Get the UTF-8 characters that represent our java string */
sx = (*env)->GetStringUTFChars(env, s, NULL);

/* Concatenate the two strings. */
concatenated = malloc(strlen("asd ") + strlen(sx) + 1);
strcpy(concatenated, "asd ");
strcat(concatenated, sx);

/* Create java string from our concatenated C string */
retval = (*env)->NewStringUTF(env, concatenated);

/* Free the memory in sx */
(*env)->ReleaseStringUTFChars(env, s, sx);

/* Free the memory in concatenated */
free(concatenated);

return retval;

这篇关于Android JNI C简单附加功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆