jni怎么样传递一个字符串数组?

aree 2013-01-25 06:09:00
怎么样传递一个字符串?
xx.cpp

JNIEXPORT jstring JNICALL Hello_Native(JNIEnv *env, jobject obj,jstring string)
{
const char *str = env->GetStringUTFChars(string, 0);
return env->NewStringUTF( "Hello from JNI !");
}
static JNINativeMethod gMethods[] = {
{"JniHello",const_cast<char*>("(Ljava/lang/jsting)Ljava/lang/jsting;"),(void*)Hello_Native},


xx.java

public native static String JniHello(String text);


系统一直提示,gMethods中声明JniHello有问题。参数不对。
...全文
434 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
DrSmart 2013-01-30
  • 打赏
  • 举报
回复
传一个字符串,到native中自己拆成字符串即可,比如strtok函数
yuhuaijun 2013-01-29
  • 打赏
  • 举报
回复
看一下以下例如,希望对你有所帮助:

private native void _setDataSource(
        String path, String[] keys, String[] values)
        throws IOException, IllegalArgumentException, SecurityException, IllegalStateException;

{
        "_setDataSource",
        "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)V",
        (void *)android_media_MediaPlayer_setDataSourceAndHeaders
}
jialiry 2013-01-29
  • 打赏
  • 举报
回复
以NDK自带hellojni为例

public class HelloJni extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/*
		 * Create a TextView and set its content. the text is retrieved by
		 * calling a native function.
		 */
		TextView tv = new TextView(this);
		tv.setText(stringFromJNI("jialiry"));
		setContentView(tv);
	}

	/*
	 * A native method that is implemented by the 'hello-jni' native library,
	 * which is packaged with this application.
	 */
	public native String stringFromJNI(String str);

	/*
	 * This is another native method declaration that is *not* implemented by
	 * 'hello-jni'. This is simply to show that you can declare as many native
	 * methods in your Java code as you want, their implementation is searched
	 * in the currently loaded native libraries only the first time you call
	 * them.
	 * 
	 * Trying to call this function will result in a
	 * java.lang.UnsatisfiedLinkError exception !
	 */
	public native String unimplementedStringFromJNI();

	/*
	 * this is used to load the 'hello-jni' library on application startup. The
	 * library has already been unpacked into
	 * /data/data/com.example.hellojni/lib/libhello-jni.so at installation time
	 * by the package manager.
	 */
	static {
		System.loadLibrary("hello-jni");
	}
}

jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
		jobject thiz, jstring jstr)
{
	char buf[128];
	const char* string=(char*)(*env)->GetStringUTFChars(env,jstr,NULL);
	strcpy(buf, string);
	strcat(buf, "同志,hello from jni");
	(*env)->ReleaseStringUTFChars(env,jstr,(jbyte*)string);
	return (*env)->NewStringUTF(env, buf);
}
结果: jialiry同志,hello from jni
雨焰 2013-01-29
  • 打赏
  • 举报
回复
我就趁着你的这个帖子看看回复吧!说不定那条对我有用!哎!没分的人好可怜!
Rayleigh 2013-01-29
  • 打赏
  • 举报
回复
static JNINativeMethod gMethods[] = {"JniHello","(Ljava/lang/String)Ljava/lang/String;",(void*)Hello_Native}, 这样改试一试
lqgyt1 2013-01-29
  • 打赏
  • 举报
回复
JNIEXPORT jstring JNICALL
Java_Prompt_getLine(JNIEnv *env, jobject obj, jstring prompt)
{
char buf[128];
const jbyte *str;
str = (*env)->GetStringUTFChars(env, prompt, NULL);
if (str == NULL) {
return NULL; /* OutOfMemoryError already thrown */
}
printf("%s", str);
(*env)->ReleaseStringUTFChars(env, prompt, str);
/* We assume here that the user does not type more than
* 127 characters */
scanf("%127s", buf);
return (*env)->NewStringUTF(env, buf);
}

class Prompt {
// native method that prints a prompt and reads a line
private native String getLine(String prompt);
public static void main(String args[]) {
Prompt p = new Prompt();
String input = p.getLine("Type a line: ");
System.out.println("User typed: " + input);
}
static {
System.loadLibrary("Prompt");
}
}
kiffa 2013-01-26
  • 打赏
  • 举报
回复
这种高额送分题都没人接。。。真可惜。。。

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧