---
title: "NDK学习 登录逻辑 C调用java方法"
author: "sfshine"
date: "2012-11-13T20:05:00Z"
tags: []
categories: ["移动开发"]
slug: "ndk-c-java"
draft: false
lastmod: "2012-11-13T21:30:16Z"
id: 1724917
url: "https://sfshine.iteye.com/blog/1724917"
---

1.登录逻辑

\[code=&quot;cpp&quot;]#include  
#include  
#include&quot;cn\_itcast\_login\_LoginActivity.h&quot;  
#include  
#include  
#define LOG\_TAG &quot;System.out.c&quot;  
#define LOGD(...) \_\_android\_log\_print(ANDROID\_LOG\_DEBUG, LOG\_TAG, \_\_VA\_ARGS\_\_)  
#define LOGI(...) \_\_android\_log\_print(ANDROID\_LOG\_INFO, LOG\_TAG, \_\_VA\_ARGS\_\_)

/\**  
\* 返回值 char* 这个代表char数组的首地址  
*  Jstring2CStr 把java中的jstring的类型转化成一个c语言中的char 字符串  
\*/  
char*   Jstring2CStr(JNIEnv*   env,   jstring   jstr)  
{  
char*   rtn   =   NULL;  
jclass   clsstring   =   (\*env)-&gt;FindClass(env,&quot;java/lang/String&quot;);  
jstring   strencode   =   (\*env)-&gt;NewStringUTF(env,&quot;GB2312&quot;);  
jmethodID   mid   =   (\*env)-&gt;GetMethodID(env,clsstring,   &quot;getBytes&quot;,   &quot;(Ljava/lang/String;)[B&quot;); //参数Strng 返回byte数组  
jbyteArray   barr=   (jbyteArray)(\*env)-&gt;CallObjectMethod(env,jstr,mid,strencode); // String .getByte(&quot;GB2312&quot;);  
jsize   alen   =   (\*env)-&gt;GetArrayLength(env,barr);  
jbyte*   ba   =   (\*env)-&gt;GetByteArrayElements(env,barr,JNI\_FALSE);  
if(alen   &gt;   0)  
{  
  rtn   =   (char\*)malloc(alen+1);         //&quot;\\0&quot;  
  memcpy(rtn,ba,alen);  
  rtn\[alen]=0;  
}  
(\*env)-&gt;ReleaseByteArrayElements(env,barr,ba,0);  //  
return rtn;  
}  
int login(char* username, char* pwd){//具体登录逻辑  
  // 连接网络发送数据给服务器,  
  // username &quot;zhangsan&quot; pwd &quot;123&quot;  
  char* rightname =&quot;zhangsan&quot;;  
  char* rightpwd =&quot;123&quot;;  
  int i=0;  
  for(;username\[i]!='\\0';i++){//判断两个字符串是否相等  
     if(username\[i]!=rightname\[i] )  
      return 404;  
  }  
   return 200;  
}

JNIEXPORT jint JNICALL Java\_cn\_itcast\_login\_LoginActivity\_call\_1login  
  (JNIEnv * env , jobject obj , jstring jname, jstring jpwd){  
char* cname = Jstring2CStr(env,jname);  
char* cpwd = Jstring2CStr(env,jpwd);  
LOGI(&quot;name =%s&quot;,cname);  
LOGI(&quot;pwd =%s&quot;,cpwd);

return login(cname,cpwd);  
}

2.C调用java方法

\[code=&quot;cpp&quot;]#include  
#include  
#include&quot;cn\_itcast\_ndkcallback\_DataProvider.h&quot;  
#include&quot;cn\_itcast\_ndkcallback\_DemoActivity.h&quot;  
#include  
#include  
#define LOG\_TAG &quot;System.out.c&quot;  
#define LOGD(...) \_\_android\_log\_print(ANDROID\_LOG\_DEBUG, LOG\_TAG, \_\_VA\_ARGS\_\_)  
#define LOGI(...) \_\_android\_log\_print(ANDROID\_LOG\_INFO, LOG\_TAG, \_\_VA\_ARGS\_\_)  
JNIEXPORT void JNICALL Java\_cn\_itcast\_ndkcallback\_DataProvider\_callmethod1  
  (JNIEnv * env, jobject obj){  
//在c代码里面调用java代码里面的方法  
// java 反射  
    //1 . 找到java代码的 class文件  
//    jclass      (\*FindClass)(JNIEnv\*, const char\*);  
jclass dpclazz = (\*env)-&gt;FindClass(env,&quot;cn/itcast/ndkcallback/DataProvider&quot;);  
if(dpclazz==0){  
LOGI(&quot;find class error&quot;);  
return;  
}  
LOGI(&quot;find class &quot;);

//2 寻找class里面的方法 用javap方法  
//   jmethodID   (\*GetMethodID)(JNIEnv\*, jclass, const char\*, const char\*);  
//参数说明:env虚拟机 dpclazz 类 &quot;hellofromjava&quot;方法名 ()V返回类型 空参数 空方法  
jmethodID method1 = (\*env)-&gt;GetMethodID(env,dpclazz,&quot;helloFromJava&quot;,&quot;()V&quot;);  
if(method1==0){  
LOGI(&quot;find method1 error&quot;);  
return;  
}  
LOGI(&quot;find method1 &quot;);  
//3 .调用这个方法 执行了java的方法  
//    void        (\*CallVoidMethod)(JNIEnv\*, jobject, jmethodID, ...);  
(\*env)-&gt;CallVoidMethod(env,obj,method1);  
}

JNIEXPORT void JNICALL Java\_cn\_itcast\_ndkcallback\_DataProvider\_callmethod2  
  (JNIEnv * env, jobject obj){  
// java 反射  
    //1 . 找到java代码的 class文件  
//    jclass      (\*FindClass)(JNIEnv\*, const char\*);  
jclass dpclazz = (\*env)-&gt;FindClass(env,&quot;cn/itcast/ndkcallback/DataProvider&quot;);  
if(dpclazz==0){  
LOGI(&quot;find class error&quot;);  
return;  
}  
LOGI(&quot;find class &quot;);

//2 寻找class里面的方法  
//   jmethodID   (\*GetMethodID)(JNIEnv\*, jclass, const char\*, const char\*);  
//两个int类型的参数 返回一个int类型  
jmethodID method2 = (\*env)-&gt;GetMethodID(env,dpclazz,&quot;Add&quot;,&quot;(II)I&quot;);  
if(method2==0){  
LOGI(&quot;find method2 error&quot;);  
return;  
}  
LOGI(&quot;find method2 &quot;);  
// 3 调用这个方法  
//    jint        (\*CallIntMethod)(JNIEnv\*, jobject, jmethodID, ...);  
//3  5 方法的两个参数  
int result = (\*env)-&gt;CallIntMethod(env,obj,method2,3,5);  
LOGI(&quot;c code  RESULT = %d&quot;,result);  
}

JNIEXPORT void JNICALL Java\_cn\_itcast\_ndkcallback\_DataProvider\_callmethod3  
  (JNIEnv * env , jobject obj){  
  //1 . 找到java代码的 class文件  
//    jclass      (\*FindClass)(JNIEnv\*, const char\*);  
jclass dpclazz = (\*env)-&gt;FindClass(env,&quot;cn/itcast/ndkcallback/DataProvider&quot;);  
if(dpclazz==0){  
LOGI(&quot;find class error&quot;);  
return;  
}  
LOGI(&quot;find class &quot;);

//2 寻找class里面的方法  
//   jmethodID   (\*GetMethodID)(JNIEnv\*, jclass, const char\*, const char\*);  
jmethodID method3 = (\*env)-&gt;GetMethodID(env,dpclazz,&quot;printString&quot;,&quot;(Ljava/lang/String;)V&quot;);  
if(method3==0){  
LOGI(&quot;find method3 error&quot;);  
return;  
}  
LOGI(&quot;find method3 &quot;);

//3 .调用这个方法  
//    void        (\*CallVoidMethod)(JNIEnv\*, jobject, jmethodID, ...);  
(\*env)-&gt;CallVoidMethod(env,obj,method3,(\*env)-&gt;NewStringUTF(env,&quot;haha in c&quot;));

}

JNIEXPORT void JNICALL Java\_cn\_itcast\_ndkcallback\_DataProvider\_callmethod4  
  (JNIEnv * env, jobject obj){  
  //1 . 找到java代码的 class文件  
//    jclass      (\*FindClass)(JNIEnv\*, const char\*);  
jclass dpclazz = (\*env)-&gt;FindClass(env,&quot;cn/itcast/ndkcallback/DataProvider&quot;);  
if(dpclazz==0){  
LOGI(&quot;find class error&quot;);  
return;  
}  
LOGI(&quot;find class &quot;);

//2 寻找class里面的方法  
//   jmethodID   (\*GetMethodID)(JNIEnv\*, jclass, const char\*, const char\*);  
// 注意 :如果要寻找的方法是静态的方法 那就不能直接去获取methodid  
//jmethodID method4 = (\*env)-&gt;GetMethodID(env,dpclazz,&quot;printStaticStr&quot;,&quot;(Ljava/lang/String;)V&quot;);  
//    jmethodID   (\*GetStaticMethodID)(JNIEnv\*, jclass, const char\*, const char\*);  
jmethodID method4 = (\*env)-&gt;GetStaticMethodID(env,dpclazz,&quot;printStaticStr&quot;,&quot;(Ljava/lang/String;)V&quot;);  
if(method4==0){  
LOGI(&quot;find method4 error&quot;);  
return;  
}  
LOGI(&quot;find method4 &quot;);

//3.调用一个静态的java方法  
//    void        (\*CallStaticVoidMethod)(JNIEnv\*, jclass, jmethodID, ...);  
(\*env)-&gt;CallStaticVoidMethod(env,dpclazz,method4,(\*env)-&gt;NewStringUTF(env,&quot;static haha in c&quot;));  
}

//obj DemoActivity  
JNIEXPORT void JNICALL Java\_cn\_itcast\_ndkcallback\_DemoActivity\_call\_1dp\_1method1  
  (JNIEnv * env, jobject obj){  
//在c代码里面调用java代码里面的方法  
// java 反射  
    //1 . 找到java代码的 class文件  
//    jclass      (\*FindClass)(JNIEnv\*, const char\*);  
jclass dpclazz = (\*env)-&gt;FindClass(env,&quot;cn/itcast/ndkcallback/DataProvider&quot;);  
if(dpclazz==0){  
LOGI(&quot;find class error&quot;);  
return;  
}  
LOGI(&quot;find class &quot;);

//2 寻找class里面的方法  
//   jmethodID   (\*GetMethodID)(JNIEnv\*, jclass, const char\*, const char\*);  
jmethodID method1 = (\*env)-&gt;GetMethodID(env,dpclazz,&quot;helloFromJava&quot;,&quot;()V&quot;);  
if(method1==0){  
LOGI(&quot;find method1 error&quot;);  
return;  
}  
LOGI(&quot;find method1 &quot;);  
//3 .调用这个方法 创建一个新的obj对象?  
//    void        (\*CallVoidMethod)(JNIEnv\*, jobject, jmethodID, ...);  
//    jobject     (\*NewObject)(JNIEnv\*, jclass, jmethodID, ...);  
//  jobject     (\*AllocObject)(JNIEnv\*, jclass);  
jobject dpobj= (\*env)-&gt;AllocObject(env,dpclazz);

(\*env)-&gt;CallVoidMethod(env,dpobj,method1);  
}

\[code=&quot;java&quot;]package cn.itcast.ndkcallback;

public class DataProvider {  
//C调用java空方法  
public void helloFromJava(){

System.out.println(&quot;hello from java&quot;);  
}  
//C调用java中的带两个int参数的方法  
public int Add(int x,int y){  
int result = x+y;  
System.out.println(&quot;java result&quot;+ result);  
return result;  
}  
//C调用java中参数为string的方法  
public void printString(String s){  
System.out.println(&quot;java &quot;+ s);  
}

public static void printStaticStr(String s){  
System.out.println(&quot;java static&quot;+ s);  
}

//让c代码调用对应的java代码  
public native void callmethod1();  
public native void callmethod2();  
public native void callmethod3();  
//调用一个静态的c代码  
public native void callmethod4();  
}