java传送json到jni,jni传送json到java,jsonjni
分享于 点击 18198 次 点评:128
java传送json到jni,jni传送json到java,jsonjni
java传送json到jni,jni传送json到java
1.先在activity中写好如下代码:
public class MainActivity extends Activity implements OnClickListener{
private Button btnMethod;
private TextView tvPrint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_method:
String print = getStringFromJNI();
JSONObject jsonObject = getJsonObject();
String jsonString = tranformJson(jsonObject.toString());
tvPrint.setText(print+"\n"+jsonObject.toString()+"\n"+jsonString);
break;
default:
break;
}
}
private JSONObject getJsonObject(){
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("name", "张三");
jsonObject.put("gender", "男");
} catch (Exception e) {
}
return jsonObject;
}
private void initView(){
btnMethod = (Button)findViewById(R.id.btn_method);
btnMethod.setOnClickListener(this);
tvPrint = (TextView)findViewById(R.id.tv_print);
}
private static final String libSoName = "NDK_01";
public native String getStringFromJNI();
public native String tranformJson(String jsonString);
static {
System.loadLibrary(libSoName);
}
}
2.生成com_hyy_jni_MainActivity.h件,命令如下:
D:\workspace\android\JniLearn\bin\classes>javah -classpath “D:\Program Files\eclipse_sdk\sdk\platforms\android-18\android.jar”; com.hyy.jni.MainActivity
其他的删掉,留下的如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_hyy_jni_MainActivity */
#ifndef _Included_com_hyy_jni_MainActivity
#define _Included_com_hyy_jni_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_hyy_jni_MainActivity
* Method: getStringFromJNI
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_hyy_jni_MainActivity_getStringFromJNI
(JNIEnv *, jobject);
/*
* Class: com_hyy_jni_MainActivity
* Method: tranformJson
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_hyy_jni_MainActivity_tranformJson
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
3.新建jni文件;
4.把第2步生成的com_hyy_jni_MainActivity.h文件放到jni文件中;
5.在jni文件下新建jniCoreMethod.cpp文件,把com_hyy_jni_MainActivity.h文件下的方法名拷到jniCoreMethod.cpp文件里;
6.json.h,jsoncpp.cpp放到jni文件下;(这个得自己下载)
7.新建Android.mk,新建Application.mk
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_CAMERA_MODULES:=off
LOCAL_MODULE := NDK_01
LOCAL_SRC_FILES := jniCoreMethod.cpp
LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog
LOCAL_ALLOW_UNDEFINED_SYMBOLS := true
APP_PLATFORM := android-14
include $(BUILD_SHARED_LIBRARY)
Application.mk:
APP_ABI := armeabi armeabi-v7a
APP_STL := stlport_static
APP_STL := gnustl_static
APP_CPPFLAGS +=-std=c++11
APP_CPPFLAGS := -fexceptions -frtti #允许异常功能,及运行时类型识别
APP_CPPFLAGS +=-fpermissive
8.写接收json和传送json方法;,我另外新建了jsontojson.h文件,把方法写到.h文件里:
#include <android/log.h>
#include <iostream>
#include <string>
#include <jni.h>
#include <json.h>
#include <jsoncpp.cpp>
#include <cstring>
using namespace std;
string jsonStringtransform(string data){
Json::Reader reader;
Json::Value root;
if(reader.parse(data,root)){
string name = root["name"].asString();
string gender = root["gender"].asString();
__android_log_print(ANDROID_LOG_INFO, "JNIMsg", "Get Param: %s %s From Java", name.c_str(),gender.c_str());
// std::cout<<name;
// std::cout<<gender<<endl;
}
Json::Value user;
user["name"] = "c++张三";
user["gender"] = "c++男";
return user.toStyledString();
}
9.然后再jniCoreMethod.cpp文件添加此jsontojson.h,如下:
#include "com_hyy_jni_MainActivity.h"
#include "jsontojson.h"
/*
* Class: com_genepoint_jni_JniActivity
* Method: getStringFromNative
* Signature: ()Ljava/lang/String;
*/
jstring Java_com_hyy_jni_MainActivity_getStringFromJNI
(JNIEnv *env, jobject thiz){
return env->NewStringUTF("Hello From JNI!");
}
jstring Java_com_hyy_jni_MainActivity_tranformJson
(JNIEnv *env, jobject thiz, jstring code){
const char *str_data = env->GetStringUTFChars(code,0);
string tmp = jsonStringtransform(str_data);
return env->NewStringUTF(tmp.c_str());
}
然后就可以运行啦!代码已上传,
http://download.csdn.net/detail/he1454023820/9385829
有问题请留言,如果有用,点个赞哦
相关文章
- 暂无相关文章
用户点评