jni c 传递对象到java_JNI开发-C/C++传递对象到Java

JNI开发有时需要C/C++传递对象到Java,此篇主要讲解C/C++传递Student对象到Java;

1. 定义com.niubashaoye.simple.jni.StuInfo类

public class StuInfo {

private int stuId;

private String stuName;

private int stuAge;

private String className;

public StuInfo(int stuId, String stuName, int stuAge, String className) {

super();

this.stuId = stuId;

this.stuName = stuName;

this.stuAge = stuAge;

this.className = className;

}

//getter()

//setter()

@Override

public String toString() {

return "StuInfo [stuId=" + stuId + ", stuName=" + stuName + ", stuAge=" + stuAge + ", className=" + className

+ "]";

}

}

2. 添加native函数

public class JNITools {

static {

System.loadLibrary("TestDemo");

}

/**

* 获取学生信息

*

* @param stuInfo

* @return

*/

public native StuInfo getStuInfo();

} * 获取学生信息

*

* @param stuInfo

* @return

*/

public native StuInfo getStuInfo();

}

3. C/C++文件

3.1 添加StuInfo结构体

typedef struct {

int stuId;

char stuName[50];

int stuAge;

char className[50];

} StuInfo;

3.2 获取StuInfo对象

JNIEXPORT jobject JNICALL Java_com_niubashaoye_simple_jni_JNITools_getStuInfo(

JNIEnv *env, jobject obj) {

//给StuInfo结构体赋值

StuInfo stuInfo;

stuInfo.stuId = 100123;

const char *name = "我是我自己!";

strcpy(stuInfo.stuName, name);

const char *className = "高三六班";

strcpy(stuInfo.className, className);

//获取jclass的实例

jclass jcs = env->FindClass("com/niubashaoye/simple/jni/StuInfo");

//获取StuInfo的字段ID

jfieldID fileId = env->GetFieldID(jcs, "stuId", "I");

jfieldID nameId = env->GetFieldID(jcs, "stuName", "Ljava/lang/String;");

jfieldID ageId = env->GetFieldID(jcs, "stuAge", "I");

jfieldID classId = env->GetFieldID(jcs, "className", "Ljava/lang/String;");

//创建StuInfo对象

jobject stuObje = env->AllocObject(jcs);

//设置变量值

env->SetIntField(stuObje, fileId, stuInfo.stuId);

env->SetIntField(stuObje, ageId, stuInfo.stuAge);

env->SetObjectField(stuObje, nameId, env->NewStringUTF(stuInfo.stuName));

env->SetObjectField(stuObje, classId, env->NewStringUTF(stuInfo.className));

return stuObje;

}JNIEnv *env, jobject obj) {

//给StuInfo结构体赋值

StuInfo stuInfo;

stuInfo.stuId = 100123;

const char *name = "我是我自己!";

strcpy(stuInfo.stuName, name);

const char *className = "高三六班";

strcpy(stuInfo.className, className);

//获取jclass的实例

jclass jcs = env->FindClass("com/niubashaoye/simple/jni/StuInfo");

//获取StuInfo的字段ID

jfieldID fileId = env->GetFieldID(jcs, "stuId", "I");

jfieldID nameId = env->GetFieldID(jcs, "stuName", "Ljava/lang/String;");

jfieldID ageId = env->GetFieldID(jcs, "stuAge", "I");

jfieldID classId = env->GetFieldID(jcs, "className", "Ljava/lang/String;");

//创建StuInfo对象

jobject stuObje = env->AllocObject(jcs);

//设置变量值

env->SetIntField(stuObje, fileId, stuInfo.stuId);

env->SetIntField(stuObje, ageId, stuInfo.stuAge);

env->SetObjectField(stuObje, nameId, env->NewStringUTF(stuInfo.stuName));

env->SetObjectField(stuObje, classId, env->NewStringUTF(stuInfo.className));

return stuObje;

}

4. Java调用native函数

StuInfo stuInfo = jniTools.getStuInfo();

Toast.makeText(getApplicationContext(), stuInfo.toString(), Toast.LENGTH_SHORT).show();

代码下载:https://download.csdn.net/download/niuba123456/10533705

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值