JSON is a lightweight data exchange format, which is often used in many places. When developing an android program, you must communicate with the server and use JSON parsing.
In the android SDK environment, the org. JSON library is provided, and you do not need to compile it yourself. In the ndk environment, I cannot find the built-in system (I didn't find it at all ..), Therefore, json cpp is used. When we integrate json cpp into the system, we find that there are no relevant articles on the Internet, so we can write them easily, hoping to help people who need them.
Jsoncpp is a JSON library for open-source CPP ,:
Http://sourceforge.net/projects/jsoncpp/files/jsoncpp/0.6.0-rc2/
Download jsoncpp-src-0.6.0-rc2-amalgamation.tar.gz. This version combines. h and. cpp and does not require configure. (I think it's strange to use the make system ..)
Decompress the package and put it under $ Project/JNI ($ project is the project root directory, the same below ).
To avoid repeated jsoncpp compilation in the future, we decided to compile it into a dynamic library and separate it. By the way, create a Test Program Main and put it under test. The path is as follows:
Tree $ Project/JNI/
////////////////////////////////
Android. mk
Application. mk
Jsoncpp
------- Android. mk JSON jsoncpp. cpp
Test
------- Android. mk main. cpp
(The Mac does not have the Tree Command. It will be done later)
1. jsoncpp uses STL exception, so if you specify the STL library path in the application file of the android compilation system, if you use: app_stl: = stlport_static, therefore, compilation fails. Change to: app_stl: = gnustl_static
JNI/application. mk
# it is needed for ndk-r5#APP_STL := stlport_staticAPP_STL := gnustl_staticAPP_MODULES := jsoncpp main
2. The Android. mk of the project specifies two sub-android. mk to be compiled.
JNI/Android. mk
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)subdirs := $(LOCAL_PATH)/test/Android.mksubdirs += $(LOCAL_PATH)/jsoncpp/Android.mkinclude $(subdirs)
3. Make file of jsoncpp. Note local_cppflags: =-djson_is_amalgamation-fexceptions. The macro definition json_is_amalgamation tells jsoncpp that the version is amalgamation, that is, the version we just downloaded. -Fexceptions: The exception application is enabled.
JNI/jsoncpp/Android. mk
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := jsoncppLOCAL_CPPFLAGS := -DJSON_IS_AMALGAMATION -fexceptionsLOCAL_SRC_FILES := jsoncpp.cppLOCAL_C_INCLUDES := $(LOCAL_PATH)/json# it is used for ndk-r5# if you build with ndk-r4, comment it# because the new Windows toolchain doesn't support Cygwin's drive# mapping (i.e /cygdrive/c/ instead of C:/)LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/../../libs/armeabi)include $(BUILD_SHARED_LIBRARY)
4. Make file of our test program. Note local_c_includes: = $ (local_path )/.. for the/jsoncpp line, we specify the header file address as the jsoncpp folder at the upper level of the current path ($ Project/JNI/test/), that is, $ Project/JNI/jsoncpp /, in this way, we need inlcude as "JSON/JSON. H ".
LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/../../libs/armeabi) \ -ljsoncpp -llog
The library libjsoncpp and liblog used are specified here.
JNI/test/Android. mk
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := main# for jsoncppLOCAL_CPPFLAGS := -DJSON_IS_AMALGAMATIONLOCAL_SRC_FILES := main.cppLOCAL_C_INCLUDES := $(LOCAL_PATH)/../jsoncpp# it is used for ndk-r5# if you build with ndk-r4, comment it# because the new Windows toolchain doesn't support Cygwin's drive# mapping (i.e /cygdrive/c/ instead of C:/)LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/../../libs/armeabi) \ -ljsoncpp -lloginclude $(BUILD_SHARED_LIBRARY)
5. Test the program file main. cpp. Because it is CPP, the interface with the Java function is added with extern "C", or the function name cannot be found after compilation. Jsoncpp is easy to use, that is, to define a JSON: value, and then fill in the data. It will be reloaded according to the data type.
JNI/test/Main. cpp
#include <jni.h>#include <android/log.h>#include "json/json.h"#define LOG_TAG "main"#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)extern "C"{jstring Java_com_kevin_tutorial_jsoncpp_ToUseJsoncppActivity_toBuildJson(JNIEnv* env, jobject thiz,jint id,jstring name){ jboolean isCopy = 0; const char* c_name = env->GetStringUTFChars(name, &isCopy); LOGD("on calling,id:%d,name:%s",id,c_name); // to build a json object with id and name Json::Value user; user["id"] = id; user["name"] = c_name; const char* json_str = user.toStyledString().c_str(); jstring result = env->NewStringUTF(json_str); env->ReleaseStringUTFChars(name,c_name); return result;} /* Java_com_kevin_tutorial_jsoncpp_ToUseJsoncppActivity_toBuildJson */} /* extern "C" */
6. Java interfaces. Pay attention to the sequence of loading databases:
System.loadLibrary("jsoncpp");System.loadLibrary("main");
$ Project/src/COM/Kevin/tutorial/jsoncpp/tousejsoncppactivity. Java
package com.kevin.tutorial.jsoncpp;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class ToUseJsoncppActivity extends Activity {private static final String TAG = "ToUseJsoncppActivity.java"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btnOk = (Button)findViewById(R.id.btnOk); btnOk.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubint id = 1001;String name = "Kevin";String result = toBuildJson(id, name);Log.i(TAG,String.format("Id:%1$d,Name:%2$s,The json formated string:%3$s", id,name,result));}}); } private static native String toBuildJson(int id,String name); static {System.loadLibrary("jsoncpp");System.loadLibrary("main");}}
7. Finally, compile ndk-build in the project path (environment variables of ndk need to be configured ). Note: If you have used app_stl: = stlport_static before, you need ndk-build clean before building.
Then, create ant in the android update project-P./, debug and install ant on the machine.
Mac v10.6.8, Android 2.3.3, android-ndk-r6b test passed, complete project see Attachment:
... I don't know how to upload attachments.