相信在使用jni的过程中,总是要传递各种各样的类型,在这其中,我也碰到了一些问题。
简单的传一些内容,相信在网上一搜一大把。
所以我们就来说说。传递对象中包含arraylist吧。
在这里先给大家一个连接,显示一些jni的传递:http://blog.csdn.NET/u_xtian/article/details/6033963
所以,我们定义一些Java中的类
user.java
public class User{
private long id;
private boolean isMan;
}
name.java
public class People{
private int id;
private ArrayList<User> myuser;
}
public class Hello{
static{
System.loadLibrary("user");
}
public native void SetUser(People people);
}
在C语言中:
#include<jni.h>
JNIEXPORT void JNICALL Java_Hello_SetUser(JNIEnv*,
jobject jthiz, jobject people);
JNIEXPORT void JNICALL Java_HelloJni_setUserList
(JNIEnv *env, jobject jthiz, jobject people)
{
jclass peo_cls = env->GetObjectClass(people);
if(peo_cls == NULL)
{
return ;
}
jfieldID idFieldID = env->GetFieldID(peo_cls,"id","I"); //获得得People类的属性id
jint id= env->GetIntField(people, idFieldID ); //获得属性值
jfieldID codeFieldID = env->GetFieldID(peo_cls,"myuser","Ljava/util/ArrayList;");
jobject codeobj = env->GetObjectField(people, codeFieldID);
if(codeobj == NULL)
{
return ;
}
int i;
jclass cls_arraylist = env->GetObjectClass(codeobj);
//method in class ArrayList
jmethodID arraylist_get = env->GetMethodID(cls_arraylist,"get","(I)Ljava/lang/Object;");
jmethodID arraylist_size = env->GetMethodID(cls_arraylist,"size","()I");
jint len = env->CallIntMethod(codeobj,arraylist_size);
for(i=0;i<len;i++){
jobject obj_user = env->CallObjectMethod(codeobj,arraylist_get,i);
jclass cls_user = env->GetObjectClass(obj_user);
jfieldID idFieldID = env->GetFieldID(cls_user,"id","J"); // 获得属性ID
jfieldID isManFieldID = env->GetFieldID(cls_user,"isMan","Z"); // 获得属性ID
jint id= env->GetIntField(obj_user , idFieldID );//获得属性值
jboolean isMan= env->GetBooleanField(obj_user ,isManFieldID );//获得属性值
}
这样就完成了。在对象object中包含有arraylist的对象。