need:
First of all, let’s talk about the requirements. Recently, I took over a project and had to parse binary data at the bottom. The data structure is more responsible. Since the server is a C++ server, and the mobile development was not considered at the beginning, the protocol is a bit nonsense! Generally speaking, a data packet contains a vector in the map and then serializes it into binary! It's easy to make a little easier for iOS. Object-c itself supports C++, and it's just simple to encapsulate it. Android is a bit troublesome and needs to convert data into Java data format. The problem is, the data interface of C++ is generally as follows:
std::map<std::string,std::vector<struct info> > tmep;
question:
To convert it into Java data objects, I searched online. There is no information in this regard. Some of them are basically simple objects to return. It is not so complicated and I can't do it myself. My approach is like this:
Declare an object class in java, as follows:
public class ControlDictionaryValue {
public String value;
}
public class LoginMsg extends BaseMsg {
// Dictionary list
public HashMap<String, ArrayList<ControlDictionaryValue> > controlDictionary;
}
Return this object through jni, the code of jni is as follows:
JNIEXPORT job JNICALL Java_com_example_filltriangle_GL2JNILib_test(JNIEnv* env, jclass tis)
{
jclass m_login_msg = env->FindClass("hzcw/msgdata/LoginMsg");
jmethodID m_method_login_msg_init = env->GetMethodID(m_login_msg,"<init>","()V");
jobject m_login_msg_obj = env->NewObject(m_login_msg, m_method_login_msg_init);
jfieldID m_fid_controlDictionary = env->GetFieldID(m_login_msg,"controlDictionary","Ljava/util/HashMap;");
// new hashmap object
jclass class_hashmap=env->FindClass("java/util/HashMap");
jmethodID hashmap_construct_method=env->GetMethodID(class_hashmap, "<init>","()V");
job obj_hashmap =env->NewObject(class_hashmap, hashmap_construct_method, "");
jmethodID hashmap_put_method= env->GetMethodID(class_hashmap,"put","(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
// new an ArrayList object
jclass class_arraylist=env->FindClass("java/util/ArrayList");
jmethodID arraylist_construct_method=env->GetMethodID(class_arraylist, "<init>","()V");
job obj_arraylist =env->NewObject(class_arraylist, arraylist_construct_method, "");
jmethodID arraylist_add_method= env->GetMethodID(class_arraylist,"add","(Ljava/lang/Object;)Z");
// new a ControlDictionaryValue object
jclass m_cls_ControlDictionaryValue = env->FindClass("hzcw/datastruct/ControlDictionaryValue");
jmethodID m_mid_ControlDictionaryValue = env->GetMethodID(m_cls_ControlDictionaryValue,"<init>","()V");
jobject m_obj_ControlDictionaryValue = env->NewObject(m_cls_ControlDictionaryValue, m_mid_ControlDictionaryValue, "");
// Get the value variable of the ControlDictionaryValue object
jfieldID m_fid_ControlDictionaryValue_value = env->GetFieldID(m_cls_ControlDictionaryValue,"value","Ljava/lang/String;");
env->SetObjectField(m_obj_ControlDictionaryValue,m_fid_ControlDictionaryValue_value,env->NewStringUTF("Dentistry"));
env->CallBooleanMethod(obj_arraylist, arraylist_add_method, m_obj_ControlDictionaryValue);
env->CallObjectMethod(obj_hashmap,hashmap_put_method,env->NewStringUTF((char*)"Department"), obj_arraylist);
env->SetObjectField(m_login_msg_obj,m_fid_controlDictionary,obj_hashmap);
env->DeleteLocalRef(m_obj_ControlDictionaryValue); //Delete local references
env->DeleteLocalRef(obj_arraylist); //Delete local references
env->DeleteLocalRef(obj_hashmap); //Delete local references
return m_login_msg_obj;
}
The above is fake data, fill in the real data yourself, and you can understand it at a glance when doing development!