NDKで開発中の比較文字列が等しいかどうか、


/**

      Java String          

*/

char*  Jstring2CStr(JNIEnv*  env,jstring  jstr)
{
    LOGI("enter CStr2Jstring 1");
    char*   rtn   =   NULL;
    jclass   clsstring   =   (*env)->FindClass(env,"java/lang/String");
    jstring   strencode   =  (*env)->NewStringUTF(env,"GB2312");//   Cstring GB2312,  ISO8859-1
    //jmethodID   (*GetMethodID)(JNIEnv*, jclass, const char*, const char*);         ,      getBytes    
    //    :javap -s java/lang/String:   (Ljava/lang/String;)[B
    jmethodID   mid   =   (*env)->GetMethodID(env,clsstring,"getBytes","(Ljava/lang/String;)[B");
    //         String.getByte("GB2312");
    // jstring       
    LOGI("enter CStr2Jstring 2");
    // Java String getByte   jstring   Cstring     
    jbyteArray  barr=   (jbyteArray) (*env)->CallObjectMethod(env,jstr,mid,strencode);
    LOGI("enter CStr2Jstring 3");
    jsize   alen   =   (*env)->GetArrayLength(env,barr);
    LOGI("enter CStr2Jstring 4");
    jbyte*   ba   =   (*env)->GetByteArrayElements(env,barr,JNI_FALSE);
    LOGI("alen=%d
"
,alen); if(alen > 0) { rtn = (char*)malloc(alen+1+128); LOGI("rtn address == %p",&rtn);// rtn memcpy(rtn,ba,alen); rtn[alen]=0; //"\0" } (*env)->ReleaseByteArrayElements(env,barr,ba,0); return rtn; } /** * */ JNIEXPORT jint JNICALL Java_com_org_vincent_javacallc_JNI_checkPwd (JNIEnv * env, jobject job, jstring js){ const char *fromJava= Jstring2CStr(env,js); // 1557 LOGI("fromJava =%s",fromJava); char *c="1557"; int resultCode; resultCode = strcmp(fromJava,c); LOGI("resultCode = %d",resultCode); // (*env)->ReleaseStringCritical(env,js,fromJava); if (resultCode==0) return 1; else return 0; } // JNIEXPORT jint JNICALL Java_com_org_vincent_javacallc_JNI_checkPwd (JNIEnv * env, jobject job, jstring js){ LOGI("javaString==%s",(*env)->GetStringUTFChars(env,js,JNI_FALSE)); // Jstring2CStr , jni GetStringUTFChars, GetStringChars 。 const char *fromJava= (*env)->GetStringUTFChars(env,js,JNI_FALSE); // 1557 LOGI("fromJava =%s",fromJava); char *c="1557"; int resultCode; resultCode = strcmp(fromJava,c); LOGI("resultCode = %d",resultCode); (*env)->ReleaseStringUTFChars(env,js,fromJava); if (resultCode==0) return 1; else return 0; }