重庆 网站备案,大型门户网站开发,wordpress链接尾缀,百度推广没有一点效果基于JNI 实现 嵌套 List 类型参数解析 背景分析解决 背景
在前面两篇文章中#xff0c;我们总结了Java 调用 C/C SDK 的几种方案#xff0c;分享了JNI在实践过程中的一些踩坑点#xff0c;而在这篇文章将继续分享针对Java List类型及其嵌套类型#xff0c;我们的JNI如何接… 基于JNI 实现 嵌套 List 类型参数解析 背景分析解决 背景
在前面两篇文章中我们总结了Java 调用 C/C SDK 的几种方案分享了JNI在实践过程中的一些踩坑点而在这篇文章将继续分享针对Java List类型及其嵌套类型我们的JNI如何接收。
分析
如下是我的的SDK的结构体定义
struct CustomParam{std::string key;std::vectorstd::string values;
};SDK的请求体入参定义如下
struct Request
{std::string ref_text;std::vectorCustomParam word_list; Request operator(const Request other){ref_text other.ref_text;word_list other.word_list;return *this;}
};解决
● 根据上面的SDK结构体的相关定义我们在Java层可以有如下的 类型定义
public class CustomParam {String key ;ListString values new ArrayList();public String getKey() {return key;}public void setWord(String key) {this.key key;}public ListString getValues() {return values;}public void setValues(ListString values) {this.values values;}
}● 关于native的方法申明如下
public class CustomParamNative {public static native int starts(ArrayListObject customParam);}
● 根据native方法生成 头部文件 javac -encoding utf8 -h . XXX.java ● 如下为生成的native方法的头部文件
/* DO NOT EDIT THIS FILE - it is machine generated */
#include jni.h
/* Header for class com_study_core_jni_CustomParamNative */#ifndef _Included_com_study_core_jni_CustomParamNative
#define _Included_com_study_core_jni_CustomParamNative
#ifdef __cplusplus
extern C {
#endif/** Class: com_study_core_jni_CustomParamNative* Method: starts* Signature: (JLjava/lang/String;Ljava/lang/String;F)I*/
JNIEXPORT jint JNICALL Java_com_study_core_jni_CustomParamNative_starts(JNIEnv *, jclass, jlong, jstring, jstring, jfloat);#ifdef __cplusplus
}
#endif
#endif
● 头部文件的具体实现
#include com_study_core_jni_CustomParamNative.h
#include vector
#undef __request
#define __request Request#include stdio.h
#include stdlib.hJNIEXPORT jint JNICALL Java_com_study_core_jni_CustomParamNative_starts(JNIEnv *env, jclass obj, jstring jni_ref, jobject customParamList)
{__request request;CopyString(env, thread-text_buff, jni_ref);request.ref_text thread-text_buff.data();CopyString(env, thread-text_buff, jni_utt);request.audio_id thread-text_buff.data();if (customParamList ! NULL){// 获取ArrayList类和对应的方法IDjclass arrayListClass env-FindClass(java/util/ArrayList);jmethodID getMethodID env-GetMethodID(arrayListClass, get, (I)Ljava/lang/Object;);jmethodID sizeMethodID env-GetMethodID(arrayListClass, size, ()I);// 获取CustomParam类和对应的字段IDjclass customParamClass env-FindClass(com/seewo/study/core/bo/CustomParam);jfieldID wordFieldID env-GetFieldID(customParamClass, key, Ljava/lang/String;);jfieldID valuesFieldID env-GetFieldID(customParamClass, values, Ljava/util/List;);// 获取ArrayList的大小jint size env-CallIntMethod(customParamList, sizeMethodID);printf(size %d\n, size);// 遍历ArrayList并解析CustomParam对象for (int i 0; i size; i){// 获取CustomParam对象jobject customParamObj env-CallObjectMethod(customParamList, getMethodID, i);// 获取key字段的值jstring wordString (jstring)env-GetObjectField(customParamObj, wordFieldID);printf(字符 %c\n, wordString);jboolean is_copy;const char *word env-GetStringUTFChars(wordString, is_copy);printf(字符 %c\n, word);// 将word和values赋值给C结构体// 创建CustomParam结构体对象CustomParam customParam;// 将word字段的值赋值给C结构体customParam.word word;// 获取values字段的值jobject valuesList env-GetObjectField(customParamObj, valuesFieldID);jclass listClass env-FindClass(java/util/List);jmethodID toArrayMethodID env-GetMethodID(listClass, toArray, ()[Ljava/lang/Object;);jobjectArray valuesArray (jobjectArray)env-CallObjectMethod(valuesList, toArrayMethodID);jsize size env-GetArrayLength(valuesArray);std::vectorstd::string values;for (int j 0; j size; j){jstring valuesString (jstring)env-GetObjectArrayElement(valuesArray, j);const char *pron env-GetStringUTFChars(valuesString, is_copy);values.push_back(pron);env-ReleaseStringUTFChars(valuesString, pron);env-DeleteLocalRef(valuesString);}customParam.values values;request.word_list.push_back(customParam);// 释放资源env-DeleteLocalRef(customParamObj);env-ReleaseStringUTFChars(wordString, word);env-DeleteLocalRef(wordString);env-DeleteLocalRef(valuesList);env-DeleteLocalRef(valuesArray);}// 释放资源env-DeleteLocalRef(arrayListClass);env-DeleteLocalRef(customParamClass);return ThreadHandleStarts__request(request);}}
文章转载自: http://www.morning.homayy.com.gov.cn.homayy.com http://www.morning.ylxgw.cn.gov.cn.ylxgw.cn http://www.morning.djgrg.cn.gov.cn.djgrg.cn http://www.morning.xqzrg.cn.gov.cn.xqzrg.cn http://www.morning.jlschmy.com.gov.cn.jlschmy.com http://www.morning.rnxw.cn.gov.cn.rnxw.cn http://www.morning.rnribht.cn.gov.cn.rnribht.cn http://www.morning.rnyhx.cn.gov.cn.rnyhx.cn http://www.morning.neletea.com.gov.cn.neletea.com http://www.morning.wnhgb.cn.gov.cn.wnhgb.cn http://www.morning.xjnjb.cn.gov.cn.xjnjb.cn http://www.morning.chzqy.cn.gov.cn.chzqy.cn http://www.morning.hkswt.cn.gov.cn.hkswt.cn http://www.morning.nyqxy.cn.gov.cn.nyqxy.cn http://www.morning.mlzyx.cn.gov.cn.mlzyx.cn http://www.morning.supera.com.cn.gov.cn.supera.com.cn http://www.morning.ywgrr.cn.gov.cn.ywgrr.cn http://www.morning.ktrh.cn.gov.cn.ktrh.cn http://www.morning.tknqr.cn.gov.cn.tknqr.cn http://www.morning.rqknq.cn.gov.cn.rqknq.cn http://www.morning.xlbtz.cn.gov.cn.xlbtz.cn http://www.morning.gtylt.cn.gov.cn.gtylt.cn http://www.morning.kbqbx.cn.gov.cn.kbqbx.cn http://www.morning.mbmtn.cn.gov.cn.mbmtn.cn http://www.morning.rkjb.cn.gov.cn.rkjb.cn http://www.morning.fbbpj.cn.gov.cn.fbbpj.cn http://www.morning.mywnk.cn.gov.cn.mywnk.cn http://www.morning.twwts.com.gov.cn.twwts.com http://www.morning.wtcbl.cn.gov.cn.wtcbl.cn http://www.morning.rbbgh.cn.gov.cn.rbbgh.cn http://www.morning.ypbp.cn.gov.cn.ypbp.cn http://www.morning.byzpl.cn.gov.cn.byzpl.cn http://www.morning.wrtw.cn.gov.cn.wrtw.cn http://www.morning.hhfwj.cn.gov.cn.hhfwj.cn http://www.morning.mhcft.cn.gov.cn.mhcft.cn http://www.morning.qxwrd.cn.gov.cn.qxwrd.cn http://www.morning.bxrqf.cn.gov.cn.bxrqf.cn http://www.morning.xyhql.cn.gov.cn.xyhql.cn http://www.morning.ejknty.cn.gov.cn.ejknty.cn http://www.morning.rjmb.cn.gov.cn.rjmb.cn http://www.morning.qtkdn.cn.gov.cn.qtkdn.cn http://www.morning.lbgsh.cn.gov.cn.lbgsh.cn http://www.morning.pwppk.cn.gov.cn.pwppk.cn http://www.morning.kwqt.cn.gov.cn.kwqt.cn http://www.morning.ybnps.cn.gov.cn.ybnps.cn http://www.morning.knlbg.cn.gov.cn.knlbg.cn http://www.morning.kjlia.com.gov.cn.kjlia.com http://www.morning.trnhy.cn.gov.cn.trnhy.cn http://www.morning.hwprz.cn.gov.cn.hwprz.cn http://www.morning.cywf.cn.gov.cn.cywf.cn http://www.morning.zcncb.cn.gov.cn.zcncb.cn http://www.morning.htbgz.cn.gov.cn.htbgz.cn http://www.morning.rbmm.cn.gov.cn.rbmm.cn http://www.morning.lrzst.cn.gov.cn.lrzst.cn http://www.morning.qfgxk.cn.gov.cn.qfgxk.cn http://www.morning.bnlsd.cn.gov.cn.bnlsd.cn http://www.morning.msgcj.cn.gov.cn.msgcj.cn http://www.morning.qszyd.cn.gov.cn.qszyd.cn http://www.morning.djbhz.cn.gov.cn.djbhz.cn http://www.morning.ktbjk.cn.gov.cn.ktbjk.cn http://www.morning.pzcjq.cn.gov.cn.pzcjq.cn http://www.morning.rxfjg.cn.gov.cn.rxfjg.cn http://www.morning.qmncj.cn.gov.cn.qmncj.cn http://www.morning.fhyhr.cn.gov.cn.fhyhr.cn http://www.morning.kxxld.cn.gov.cn.kxxld.cn http://www.morning.bpmdh.cn.gov.cn.bpmdh.cn http://www.morning.qlkzl.cn.gov.cn.qlkzl.cn http://www.morning.cfjyr.cn.gov.cn.cfjyr.cn http://www.morning.qfkdt.cn.gov.cn.qfkdt.cn http://www.morning.bsbcp.cn.gov.cn.bsbcp.cn http://www.morning.kjyhh.cn.gov.cn.kjyhh.cn http://www.morning.rntgy.cn.gov.cn.rntgy.cn http://www.morning.rttxx.cn.gov.cn.rttxx.cn http://www.morning.fpzz1.cn.gov.cn.fpzz1.cn http://www.morning.knrgb.cn.gov.cn.knrgb.cn http://www.morning.lsssx.cn.gov.cn.lsssx.cn http://www.morning.prhfc.cn.gov.cn.prhfc.cn http://www.morning.lmrjn.cn.gov.cn.lmrjn.cn http://www.morning.gtdf.cn.gov.cn.gtdf.cn http://www.morning.gynls.cn.gov.cn.gynls.cn