From 979700e3431987f238ae4f9f90f087656fef63a0 Mon Sep 17 00:00:00 2001 From: jianglong0156 Date: Tue, 4 Aug 2015 11:30:30 +0800 Subject: [PATCH 1/5] there is a bug for getStringUTFChars function. it can't translate the emotion utf8 code. so change the getStringUTFChars to getStringChars. and translate unicode to utf8 in c++ program --- cocos/network/HttpClient-android.cpp | 13 +++++--- .../Java_org_cocos2dx_lib_Cocos2dxBitmap.cpp | 8 +++-- ...Java_org_cocos2dx_lib_Cocos2dxRenderer.cpp | 10 ++++-- cocos/platform/android/jni/JniHelper.cpp | 10 ++++-- .../android/CCJavascriptJavaBridge.cpp | 32 +++++++++++++------ .../platform/android/CCLuaJavaBridge.cpp | 19 +++++++---- ...org_cocos2dx_lib_Cocos2dxLuaJavaBridge.cpp | 11 +++++-- 7 files changed, 72 insertions(+), 31 deletions(-) diff --git a/cocos/network/HttpClient-android.cpp b/cocos/network/HttpClient-android.cpp index 8baa285112..4e99b7b39c 100644 --- a/cocos/network/HttpClient-android.cpp +++ b/cocos/network/HttpClient-android.cpp @@ -35,6 +35,8 @@ #include "platform/CCFileUtils.h" #include "platform/android/jni/JniHelper.h" +#include "base/ccUTF8.h" + NS_CC_BEGIN namespace network { @@ -582,13 +584,16 @@ private: const char* str = nullptr; char* ret = nullptr; - str = env->GetStringUTFChars(jstr, nullptr); - if (nullptr != str) + const unsigned short * unicodeChar = (const unsigned short *)env->GetStringChars(jstr, nullptr); + std::u16string unicodeStr((char16_t *)unicodeChar); + std::string strValue; + cocos2d::StringUtils::UTF16ToUTF8(unicodeStr, strValue); + if (strValue.length() > 0) { - ret = strdup(str); + ret = strdup(strValue.c_str()); } - env->ReleaseStringUTFChars(jstr, str); + env->ReleaseStringChars(jstr, unicodeChar); return ret; } diff --git a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxBitmap.cpp b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxBitmap.cpp index 47f0453592..f1d6d509fd 100644 --- a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxBitmap.cpp +++ b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxBitmap.cpp @@ -29,6 +29,7 @@ THE SOFTWARE. #include "../CCApplication.h" #include "platform/CCFileUtils.h" #include +#include "base/ccUTF8.h" using namespace cocos2d; @@ -59,10 +60,11 @@ std::string getStringWithEllipsisJni(const char* text, float width, float fontSi } jstring retFromJava = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, stringArg1, width, fontSize); - const char* str = t.env->GetStringUTFChars(retFromJava, 0); - ret = str; + const unsigned short * str = (const unsigned short *)t.env->GetStringChars(retFromJava, nullptr); + std::u16string unicodeStr((char16_t *)str); + cocos2d::StringUtils::UTF16ToUTF8(unicodeStr, ret); - t.env->ReleaseStringUTFChars(retFromJava, str); + t.env->ReleaseStringChars(retFromJava, str); t.env->DeleteLocalRef(stringArg1); t.env->DeleteLocalRef(t.classID); } diff --git a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxRenderer.cpp b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxRenderer.cpp index b442f9df7d..91fb78a009 100644 --- a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxRenderer.cpp +++ b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxRenderer.cpp @@ -8,6 +8,8 @@ #include "JniHelper.h" #include +#include "base/ccUTF8.h" + using namespace cocos2d; extern "C" { @@ -33,9 +35,13 @@ extern "C" { } JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInsertText(JNIEnv* env, jobject thiz, jstring text) { - const char* pszText = env->GetStringUTFChars(text, NULL); + const unsigned short * unicodeChar = (const unsigned short *)env->GetStringChars(text, nullptr); + std::u16string unicodeStr((char16_t *)unicodeChar); + std::string strValue; + cocos2d::StringUtils::UTF16ToUTF8(unicodeStr, strValue); + const char* pszText = strValue.c_str(); cocos2d::IMEDispatcher::sharedDispatcher()->dispatchInsertText(pszText, strlen(pszText)); - env->ReleaseStringUTFChars(text, pszText); + env->ReleaseStringChars(text, unicodeChar); } JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeDeleteBackward(JNIEnv* env, jobject thiz) { diff --git a/cocos/platform/android/jni/JniHelper.cpp b/cocos/platform/android/jni/JniHelper.cpp index c8ec46c546..3fccbbc501 100644 --- a/cocos/platform/android/jni/JniHelper.cpp +++ b/cocos/platform/android/jni/JniHelper.cpp @@ -27,6 +27,8 @@ THE SOFTWARE. #include #include +#include "base/ccUTF8.h" + #define LOG_TAG "JniHelper" #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) @@ -268,9 +270,11 @@ namespace cocos2d { return nullptr; } - const char* chars = env->GetStringUTFChars(jstr, nullptr); - std::string ret(chars); - env->ReleaseStringUTFChars(jstr, chars); + const unsigned short * unicodeChar = (const unsigned short *)env->GetStringChars(jstr, nullptr); + std::u16string unicodeStr((char16_t *)unicodeChar); + std::string ret; + cocos2d::StringUtils::UTF16ToUTF8(unicodeStr, ret); + env->ReleaseStringChars(jstr, unicodeChar); return ret; } diff --git a/cocos/scripting/js-bindings/manual/platform/android/CCJavascriptJavaBridge.cpp b/cocos/scripting/js-bindings/manual/platform/android/CCJavascriptJavaBridge.cpp index 0bc811356a..c0045fc9aa 100644 --- a/cocos/scripting/js-bindings/manual/platform/android/CCJavascriptJavaBridge.cpp +++ b/cocos/scripting/js-bindings/manual/platform/android/CCJavascriptJavaBridge.cpp @@ -27,6 +27,7 @@ #include "spidermonkey_specifics.h" #include "ScriptingCore.h" #include "js_manual_conversions.h" +#include "base/ccUTF8.h" #define LOG_TAG "CCJavascriptJavaBridge" #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) @@ -38,9 +39,13 @@ extern "C" { JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxJavascriptJavaBridge_evalString (JNIEnv *env, jclass cls, jstring value) { - const char *_value = env->GetStringUTFChars(value, NULL); - ScriptingCore::getInstance()->evalString(_value,NULL); - env->ReleaseStringUTFChars(value, _value); + + const unsigned short * unicodeChar = (const unsigned short *)env->GetStringChars(value, nullptr); + std::u16string unicodeStr((char16_t *)unicodeChar); + std::string strValue; + cocos2d::StringUtils::UTF16ToUTF8(unicodeStr, strValue); + ScriptingCore::getInstance()->evalString(strValue.c_str(), nullptr); + env->ReleaseStringChars(value, unicodeChar); return 1; } @@ -79,9 +84,13 @@ bool JavascriptJavaBridge::CallInfo::execute(void) case TypeString: m_retjstring = (jstring)m_env->CallStaticObjectMethod(m_classID, m_methodID); - const char *stringBuff = m_env->GetStringUTFChars(m_retjstring, 0); - m_ret.stringValue = new string(stringBuff); - m_env->ReleaseStringUTFChars(m_retjstring, stringBuff); + const unsigned short * unicodeChar = (const unsigned short *)m_env->GetStringChars(m_retjstring, nullptr); + std::u16string unicodeStr((char16_t *)unicodeChar); + std::string strValue; + cocos2d::StringUtils::UTF16ToUTF8(unicodeStr, strValue); + + m_ret.stringValue = new string(strValue); + m_env->ReleaseStringChars(m_retjstring, unicodeChar); break; } @@ -119,9 +128,12 @@ bool JavascriptJavaBridge::CallInfo::executeWithArgs(jvalue *args) case TypeString: m_retjstring = (jstring)m_env->CallStaticObjectMethodA(m_classID, m_methodID, args); - const char *stringBuff = m_env->GetStringUTFChars(m_retjstring, 0); - m_ret.stringValue = new string(stringBuff); - m_env->ReleaseStringUTFChars(m_retjstring, stringBuff); + const unsigned short * unicodeChar = (const unsigned short *)m_env->GetStringChars(m_retjstring, nullptr); + std::u16string unicodeStr((char16_t *)unicodeChar); + std::string strValue; + cocos2d::StringUtils::UTF16ToUTF8(unicodeStr, strValue); + m_ret.stringValue = new string(strValue); + m_env->ReleaseStringChars(m_retjstring, unicodeChar); break; } @@ -429,4 +441,4 @@ void JavascriptJavaBridge::_js_register(JSContext *cx, JS::HandleObject global) JavascriptJavaBridge::js_parent = NULL; JavascriptJavaBridge::js_proto = JS_InitClass(cx, global, JS::NullPtr(), &JavascriptJavaBridge::js_class , JavascriptJavaBridge::_js_constructor, 0, props, funcs, NULL, NULL); -} \ No newline at end of file +} diff --git a/cocos/scripting/lua-bindings/manual/platform/android/CCLuaJavaBridge.cpp b/cocos/scripting/lua-bindings/manual/platform/android/CCLuaJavaBridge.cpp index 14b48ddd17..f36a7c024a 100644 --- a/cocos/scripting/lua-bindings/manual/platform/android/CCLuaJavaBridge.cpp +++ b/cocos/scripting/lua-bindings/manual/platform/android/CCLuaJavaBridge.cpp @@ -2,6 +2,7 @@ #include "CCLuaJavaBridge.h" #include "platform/android/jni/JniHelper.h" #include +#include "base/ccUTF8.h" #define LOG_TAG "luajc" #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) @@ -40,9 +41,12 @@ bool LuaJavaBridge::CallInfo::execute(void) case TypeString: m_retjs = (jstring)m_env->CallStaticObjectMethod(m_classID, m_methodID); - const char *stringBuff = m_env->GetStringUTFChars(m_retjs, 0); - m_ret.stringValue = new string(stringBuff); - m_env->ReleaseStringUTFChars(m_retjs, stringBuff); + const unsigned short * stringBuff = (const unsigned short *)m_env->GetStringChars(m_retjs, nullptr); + std::u16string unicodeStr((char16_t *)stringBuff); + std::string strValue; + cocos2d::StringUtils::UTF16ToUTF8(unicodeStr, strValue); + m_ret.stringValue = new string(strValue); + m_env->ReleaseStringChars(m_retjs, stringBuff); break; } @@ -80,9 +84,12 @@ bool LuaJavaBridge::CallInfo::executeWithArgs(jvalue *args) case TypeString: m_retjs = (jstring)m_env->CallStaticObjectMethodA(m_classID, m_methodID, args); - const char *stringBuff = m_env->GetStringUTFChars(m_retjs, 0); - m_ret.stringValue = new string(stringBuff); - m_env->ReleaseStringUTFChars(m_retjs, stringBuff); + const unsigned short * unicodeChar = (const unsigned short *)m_env->GetStringChars(m_retjs, nullptr); + std::u16string unicodeStr((char16_t *)unicodeChar); + std::string strValue; + cocos2d::StringUtils::UTF16ToUTF8(unicodeStr, strValue); + m_ret.stringValue = new string(strValue); + m_env->ReleaseStringChars(m_retjs, unicodeChar); break; } diff --git a/cocos/scripting/lua-bindings/manual/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge.cpp b/cocos/scripting/lua-bindings/manual/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge.cpp index 498122e6b3..e0ae1dd031 100644 --- a/cocos/scripting/lua-bindings/manual/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge.cpp +++ b/cocos/scripting/lua-bindings/manual/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge.cpp @@ -5,6 +5,8 @@ #include "CCLuaJavaBridge.h" +#include "base/ccUTF8.h" + #define LOG_TAG "Cocos2dxLuaJavaBridge_java" #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) @@ -13,9 +15,12 @@ extern "C" { JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge_callLuaFunctionWithString (JNIEnv *env, jclass cls, jint functionId, jstring value) { - const char *value_ = env->GetStringUTFChars(value, 0); - int ret = LuaJavaBridge::callLuaFunctionById(functionId, value_); - env->ReleaseStringUTFChars(value, value_); + const unsigned short * unicodeChar = (const unsigned short *)env->GetStringChars(value, nullptr); + std::u16string unicodeStr((char16_t *)unicodeChar); + std::string strValue; + cocos2d::StringUtils::UTF16ToUTF8(unicodeStr, strValue); + int ret = LuaJavaBridge::callLuaFunctionById(functionId, strValue.c_str()); + env->ReleaseStringChars(value, unicodeChar); return ret; } From 36e8e9740ad55bd2f91306463169fe8da23cdccf Mon Sep 17 00:00:00 2001 From: jianglong0156 Date: Tue, 4 Aug 2015 11:30:55 +0800 Subject: [PATCH 2/5] improve the test case --- .../org/cocos2dx/js_tests/AppActivity.java | 7 ++++++ .../src/ReflectionTest/ReflectionTest.js | 24 +++++++++++-------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/tests/js-tests/project/proj.android/src/org/cocos2dx/js_tests/AppActivity.java b/tests/js-tests/project/proj.android/src/org/cocos2dx/js_tests/AppActivity.java index 259646d2bd..6d97e1c13d 100644 --- a/tests/js-tests/project/proj.android/src/org/cocos2dx/js_tests/AppActivity.java +++ b/tests/js-tests/project/proj.android/src/org/cocos2dx/js_tests/AppActivity.java @@ -108,6 +108,13 @@ public class AppActivity extends Cocos2dxActivity { public static String getUtfStr() { final String utf8Str = "you will see emotion:💝"; + app.runOnGLThread(new Runnable() { + @Override + public void run() { + + Cocos2dxJavascriptJavaBridge.evalString("cc.log(\"" + utf8Str + "\")"); + } + }); return utf8Str; } diff --git a/tests/js-tests/src/ReflectionTest/ReflectionTest.js b/tests/js-tests/src/ReflectionTest/ReflectionTest.js index 5e02babd18..99050cd4f5 100644 --- a/tests/js-tests/src/ReflectionTest/ReflectionTest.js +++ b/tests/js-tests/src/ReflectionTest/ReflectionTest.js @@ -28,41 +28,45 @@ var ReflectionTestLayer = BaseTestLayer.extend({ _title:"jsb.reflection", _subtitle:"call java/objective-c methods in js", - + onRestartCallback:function (sender) { + ReflectionTestScene.runThisTest(); + }, ctor:function() { this._super(cc.color(0,0,0,255), cc.color(98,99,117,255)); - var label = new cc.LabelTTF("Show Alert Dialog", "Arial", 35); + var resultLabel = new cc.LabelTTF("Show emotion result", "Arial", 20); + resultLabel.setPosition(cc.p(cc.winSize.width / 2, cc.winSize.height - 130)); + this.addChild(resultLabel); + + var label = new cc.LabelTTF("Show Alert Dialog", "Arial", 30); var menuItem = new cc.MenuItemLabel(label, function(){ if(cc.sys.os == cc.sys.OS_ANDROID){ jsb.reflection.callStaticMethod("org/cocos2dx/js_tests/AppActivity", "showAlertDialog", "(Ljava/lang/String;Ljava/lang/String;)V", "How are you ?", "I'm great !"); }else if(cc.sys.os == cc.sys.OS_IOS || cc.sys.os == cc.sys.OS_OSX){ var ret = jsb.reflection.callStaticMethod("NativeOcClass","callNativeUIWithTitle:andContent:","cocos2d-js","Yes! you call a Native UI from Reflection"); - cc.log("ret val is "+ret); + resultLabel.setString("ret val is "+ret); } }, this); - var label2 = new cc.LabelTTF("CallReflectionWithBool", "Arial", 35); + var label2 = new cc.LabelTTF("CallReflectionWithBool", "Arial", 30); var menuItem2 = new cc.MenuItemLabel(label2, function(){ if(cc.sys.os == cc.sys.OS_ANDROID){ jsb.reflection.callStaticMethod("org/cocos2dx/js_tests/AppActivity", "showAlertDialog", "(Ljava/lang/String;Ljava/lang/String;Z)V", "How are you ?", "I'm great !", true); }else if(cc.sys.os == cc.sys.OS_IOS || cc.sys.os == cc.sys.OS_OSX){ var ret = jsb.reflection.callStaticMethod("NativeOcClass","callNativeUIWithTitle:andContent:addBool:","cocos2d-js","Yes! you call a Native UI from Reflection", true); - cc.log("ret val is "+ret); + resultLabel.setString("ret val is "+ret); } }, this); - var label3 = new cc.LabelTTF("CallReflectionWithUTF8Str", "Arial", 35); + var label3 = new cc.LabelTTF("CallReflectionWithUTF8Str", "Arial", 30); var menuItem3 = new cc.MenuItemLabel(label3, function(){ if(cc.sys.os == cc.sys.OS_ANDROID){ var ret = jsb.reflection.callStaticMethod("org/cocos2dx/js_tests/AppActivity", "getUtfStr", "()Ljava/lang/String;"); - jsb.reflection.callStaticMethod("org/cocos2dx/js_tests/AppActivity", "showAlertDialog", - "(Ljava/lang/String;Ljava/lang/String;)V", "Show Emotion", ret); - cc.log("result:" + ret); + resultLabel.setString("result:" + ret); }else if(cc.sys.os == cc.sys.OS_IOS || cc.sys.os == cc.sys.OS_OSX){ var ret = "you will see emotion:💝"; jsb.reflection.callStaticMethod("NativeOcClass","callNativeUIWithTitle:andContent:","Show Emotion", ret); - cc.log("result:" + ret); + resultLabel.setString("result:" + ret); } }, this); var menu = new cc.Menu(menuItem, menuItem2, menuItem3); From ee7e7d237fdd8ed764e99c8e2d6e81e63dbfad90 Mon Sep 17 00:00:00 2001 From: jianglong0156 Date: Tue, 4 Aug 2015 11:40:10 +0800 Subject: [PATCH 3/5] improve the test case --- tests/js-tests/src/ReflectionTest/ReflectionTest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/js-tests/src/ReflectionTest/ReflectionTest.js b/tests/js-tests/src/ReflectionTest/ReflectionTest.js index 99050cd4f5..b095989781 100644 --- a/tests/js-tests/src/ReflectionTest/ReflectionTest.js +++ b/tests/js-tests/src/ReflectionTest/ReflectionTest.js @@ -29,7 +29,7 @@ var ReflectionTestLayer = BaseTestLayer.extend({ _title:"jsb.reflection", _subtitle:"call java/objective-c methods in js", onRestartCallback:function (sender) { - ReflectionTestScene.runThisTest(); + new ReflectionTestScene().runThisTest(); }, ctor:function() { this._super(cc.color(0,0,0,255), cc.color(98,99,117,255)); From 00b7e98c8905b9d9b7b76a48a8f93ae1c2910c91 Mon Sep 17 00:00:00 2001 From: jianglong0156 Date: Tue, 4 Aug 2015 16:24:08 +0800 Subject: [PATCH 4/5] improve the getUTFCharsFromJavaEnv logic and fix the httpclient response message memery leak. --- cocos/base/ccUTF8.cpp | 13 +++++++ cocos/base/ccUTF8.h | 8 +++++ cocos/network/HttpClient-android.cpp | 18 ++++------ .../Java_org_cocos2dx_lib_Cocos2dxBitmap.cpp | 10 +++--- ...Java_org_cocos2dx_lib_Cocos2dxRenderer.cpp | 10 +++--- cocos/platform/android/jni/JniHelper.cpp | 14 ++++---- .../android/CCJavascriptJavaBridge.cpp | 34 +++++++++---------- .../platform/android/CCLuaJavaBridge.cpp | 20 +++++------ ...org_cocos2dx_lib_Cocos2dxLuaJavaBridge.cpp | 20 +++++------ 9 files changed, 82 insertions(+), 65 deletions(-) diff --git a/cocos/base/ccUTF8.cpp b/cocos/base/ccUTF8.cpp index e81b160dcf..aad736e2ed 100644 --- a/cocos/base/ccUTF8.cpp +++ b/cocos/base/ccUTF8.cpp @@ -155,6 +155,17 @@ bool UTF16ToUTF8(const std::u16string& utf16, std::string& outUtf8) return llvm::convertUTF16ToUTF8String(utf16, outUtf8); } +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) +CC_DLL bool getUTFCharsFromJavaEnv(JNIEnv* env, jstring srcjStr, std::string& outUtf8) +{ + const unsigned short * unicodeChar = ( const unsigned short *)env->GetStringChars(srcjStr, nullptr); + const std::u16string unicodeStr((const char16_t *)unicodeChar); + bool flag = UTF16ToUTF8(unicodeStr, outUtf8); + env->ReleaseStringChars(srcjStr, unicodeChar); + return flag; +} +#endif + std::vector getChar16VectorFromUTF16String(const std::u16string& utf16) { std::vector ret; @@ -172,6 +183,8 @@ long getCharacterCountInUTF8String(const std::string& utf8) return getUTF8StringLength((const UTF8*)utf8.c_str()); } + + } //namespace StringUtils { diff --git a/cocos/base/ccUTF8.h b/cocos/base/ccUTF8.h index a384805bd9..e933ec97de 100644 --- a/cocos/base/ccUTF8.h +++ b/cocos/base/ccUTF8.h @@ -30,6 +30,10 @@ #include #include +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) +#include "platform/android/jni/JniHelper.h" +#endif + NS_CC_BEGIN namespace StringUtils { @@ -68,6 +72,10 @@ CC_DLL bool UTF8ToUTF16(const std::string& utf8, std::u16string& outUtf16); */ CC_DLL bool UTF16ToUTF8(const std::u16string& utf16, std::string& outUtf8); +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) +CC_DLL bool getUTFCharsFromJavaEnv(JNIEnv* env, jstring srcjStr, std::string& outUtf8); +#endif + /** * @brief Trims the unicode spaces at the end of char16_t vector. */ diff --git a/cocos/network/HttpClient-android.cpp b/cocos/network/HttpClient-android.cpp index 4e99b7b39c..28852fd614 100644 --- a/cocos/network/HttpClient-android.cpp +++ b/cocos/network/HttpClient-android.cpp @@ -581,20 +581,13 @@ private: { return nullptr; } - - const char* str = nullptr; - char* ret = nullptr; - const unsigned short * unicodeChar = (const unsigned short *)env->GetStringChars(jstr, nullptr); - std::u16string unicodeStr((char16_t *)unicodeChar); - std::string strValue; - cocos2d::StringUtils::UTF16ToUTF8(unicodeStr, strValue); - if (strValue.length() > 0) + char *ret = nullptr; + std::string strValue = ""; + if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, jstr, strValue)) { ret = strdup(strValue.c_str()); } - env->ReleaseStringChars(jstr, unicodeChar); - return ret; } @@ -720,7 +713,10 @@ void HttpClient::processResponse(HttpResponse* response, char* responseMessage) } free(contentInfo); - strcpy(responseMessage, urlConnection.getResponseMessage()); + char *messageInfo = urlConnection.getResponseMessage(); + strcpy(responseMessage, messageInfo); + free(messageInfo); + urlConnection.disconnect(); // write data to HttpResponse diff --git a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxBitmap.cpp b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxBitmap.cpp index f1d6d509fd..197a8e0587 100644 --- a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxBitmap.cpp +++ b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxBitmap.cpp @@ -60,11 +60,11 @@ std::string getStringWithEllipsisJni(const char* text, float width, float fontSi } jstring retFromJava = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, stringArg1, width, fontSize); - const unsigned short * str = (const unsigned short *)t.env->GetStringChars(retFromJava, nullptr); - std::u16string unicodeStr((char16_t *)str); - cocos2d::StringUtils::UTF16ToUTF8(unicodeStr, ret); - - t.env->ReleaseStringChars(retFromJava, str); + if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(t.env, retFromJava, ret)) + { + ret = ""; + } + t.env->DeleteLocalRef(stringArg1); t.env->DeleteLocalRef(t.classID); } diff --git a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxRenderer.cpp b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxRenderer.cpp index 91fb78a009..be0f142e8e 100644 --- a/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxRenderer.cpp +++ b/cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxRenderer.cpp @@ -35,13 +35,13 @@ extern "C" { } JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInsertText(JNIEnv* env, jobject thiz, jstring text) { - const unsigned short * unicodeChar = (const unsigned short *)env->GetStringChars(text, nullptr); - std::u16string unicodeStr((char16_t *)unicodeChar); - std::string strValue; - cocos2d::StringUtils::UTF16ToUTF8(unicodeStr, strValue); + std::string strValue = ""; + if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, text, strValue)) + { + strValue = ""; + } const char* pszText = strValue.c_str(); cocos2d::IMEDispatcher::sharedDispatcher()->dispatchInsertText(pszText, strlen(pszText)); - env->ReleaseStringChars(text, unicodeChar); } JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeDeleteBackward(JNIEnv* env, jobject thiz) { diff --git a/cocos/platform/android/jni/JniHelper.cpp b/cocos/platform/android/jni/JniHelper.cpp index 3fccbbc501..c7e9b07348 100644 --- a/cocos/platform/android/jni/JniHelper.cpp +++ b/cocos/platform/android/jni/JniHelper.cpp @@ -267,16 +267,16 @@ namespace cocos2d { JNIEnv *env = JniHelper::getEnv(); if (!env) { - return nullptr; + return ""; } - const unsigned short * unicodeChar = (const unsigned short *)env->GetStringChars(jstr, nullptr); - std::u16string unicodeStr((char16_t *)unicodeChar); - std::string ret; - cocos2d::StringUtils::UTF16ToUTF8(unicodeStr, ret); - env->ReleaseStringChars(jstr, unicodeChar); + std::string strValue = ""; + if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, jstr, strValue)) + { + strValue = ""; + } - return ret; + return strValue; } } //namespace cocos2d diff --git a/cocos/scripting/js-bindings/manual/platform/android/CCJavascriptJavaBridge.cpp b/cocos/scripting/js-bindings/manual/platform/android/CCJavascriptJavaBridge.cpp index c0045fc9aa..0385864884 100644 --- a/cocos/scripting/js-bindings/manual/platform/android/CCJavascriptJavaBridge.cpp +++ b/cocos/scripting/js-bindings/manual/platform/android/CCJavascriptJavaBridge.cpp @@ -40,13 +40,13 @@ JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxJavascriptJavaBridge_evalSt (JNIEnv *env, jclass cls, jstring value) { - const unsigned short * unicodeChar = (const unsigned short *)env->GetStringChars(value, nullptr); - std::u16string unicodeStr((char16_t *)unicodeChar); - std::string strValue; - cocos2d::StringUtils::UTF16ToUTF8(unicodeStr, strValue); + std::string strValue = ""; + if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, value, strValue)) + { + CCLOG("Cocos2dxJavascriptJavaBridge_evalString error, invalid string code"); + return 0; + } ScriptingCore::getInstance()->evalString(strValue.c_str(), nullptr); - env->ReleaseStringChars(value, unicodeChar); - return 1; } @@ -84,13 +84,13 @@ bool JavascriptJavaBridge::CallInfo::execute(void) case TypeString: m_retjstring = (jstring)m_env->CallStaticObjectMethod(m_classID, m_methodID); - const unsigned short * unicodeChar = (const unsigned short *)m_env->GetStringChars(m_retjstring, nullptr); - std::u16string unicodeStr((char16_t *)unicodeChar); - std::string strValue; - cocos2d::StringUtils::UTF16ToUTF8(unicodeStr, strValue); + std::string strValue = ""; + if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(m_env, m_retjstring, strValue)) + { + strValue = ""; + } m_ret.stringValue = new string(strValue); - m_env->ReleaseStringChars(m_retjstring, unicodeChar); break; } @@ -128,13 +128,13 @@ bool JavascriptJavaBridge::CallInfo::executeWithArgs(jvalue *args) case TypeString: m_retjstring = (jstring)m_env->CallStaticObjectMethodA(m_classID, m_methodID, args); - const unsigned short * unicodeChar = (const unsigned short *)m_env->GetStringChars(m_retjstring, nullptr); - std::u16string unicodeStr((char16_t *)unicodeChar); - std::string strValue; - cocos2d::StringUtils::UTF16ToUTF8(unicodeStr, strValue); + std::string strValue = ""; + if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(m_env, m_retjstring, strValue)) + { + strValue = ""; + } m_ret.stringValue = new string(strValue); - m_env->ReleaseStringChars(m_retjstring, unicodeChar); - break; + break; } if (m_env->ExceptionCheck() == JNI_TRUE) diff --git a/cocos/scripting/lua-bindings/manual/platform/android/CCLuaJavaBridge.cpp b/cocos/scripting/lua-bindings/manual/platform/android/CCLuaJavaBridge.cpp index f36a7c024a..555bc124ac 100644 --- a/cocos/scripting/lua-bindings/manual/platform/android/CCLuaJavaBridge.cpp +++ b/cocos/scripting/lua-bindings/manual/platform/android/CCLuaJavaBridge.cpp @@ -41,12 +41,12 @@ bool LuaJavaBridge::CallInfo::execute(void) case TypeString: m_retjs = (jstring)m_env->CallStaticObjectMethod(m_classID, m_methodID); - const unsigned short * stringBuff = (const unsigned short *)m_env->GetStringChars(m_retjs, nullptr); - std::u16string unicodeStr((char16_t *)stringBuff); - std::string strValue; - cocos2d::StringUtils::UTF16ToUTF8(unicodeStr, strValue); + std::string strValue = ""; + if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(m_env, m_retjs, strValue)) + { + strValue = ""; + } m_ret.stringValue = new string(strValue); - m_env->ReleaseStringChars(m_retjs, stringBuff); break; } @@ -84,12 +84,12 @@ bool LuaJavaBridge::CallInfo::executeWithArgs(jvalue *args) case TypeString: m_retjs = (jstring)m_env->CallStaticObjectMethodA(m_classID, m_methodID, args); - const unsigned short * unicodeChar = (const unsigned short *)m_env->GetStringChars(m_retjs, nullptr); - std::u16string unicodeStr((char16_t *)unicodeChar); - std::string strValue; - cocos2d::StringUtils::UTF16ToUTF8(unicodeStr, strValue); + std::string strValue = ""; + if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(m_env, m_retjs, strValue)) + { + strValue = ""; + } m_ret.stringValue = new string(strValue); - m_env->ReleaseStringChars(m_retjs, unicodeChar); break; } diff --git a/cocos/scripting/lua-bindings/manual/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge.cpp b/cocos/scripting/lua-bindings/manual/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge.cpp index e0ae1dd031..d2f78529e8 100644 --- a/cocos/scripting/lua-bindings/manual/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge.cpp +++ b/cocos/scripting/lua-bindings/manual/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge.cpp @@ -15,23 +15,23 @@ extern "C" { JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge_callLuaFunctionWithString (JNIEnv *env, jclass cls, jint functionId, jstring value) { - const unsigned short * unicodeChar = (const unsigned short *)env->GetStringChars(value, nullptr); - std::u16string unicodeStr((char16_t *)unicodeChar); - std::string strValue; - cocos2d::StringUtils::UTF16ToUTF8(unicodeStr, strValue); + std::string strValue = ""; + if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, value, strValue)) + { + strValue = ""; + } int ret = LuaJavaBridge::callLuaFunctionById(functionId, strValue.c_str()); - env->ReleaseStringChars(value, unicodeChar); return ret; } JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge_callLuaGlobalFunctionWithString (JNIEnv *env, jclass cls, jstring luaFunctionName, jstring value) { - const char *luaFunctionName_ = env->GetStringUTFChars(luaFunctionName, 0); - const char *value_ = env->GetStringUTFChars(value, 0); - int ret = LuaJavaBridge::callLuaGlobalFunction(luaFunctionName_, value_); - env->ReleaseStringUTFChars(luaFunctionName, luaFunctionName_); - env->ReleaseStringUTFChars(value, value_); + std::string functionNameStr = ""; + std::string valueStr = ""; + cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, luaFunctionName, functionNameStr); + cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, value, valueStr); + int ret = LuaJavaBridge::callLuaGlobalFunction(functionNameStr.c_str(), valueStr.c_str()); return ret; } From 3230ca154df690114ea38bbd99706b807da96cfb Mon Sep 17 00:00:00 2001 From: jianglong0156 Date: Tue, 4 Aug 2015 16:42:16 +0800 Subject: [PATCH 5/5] improve the code. fix network error --- cocos/base/ccUTF8.cpp | 2 -- cocos/network/HttpClient-android.cpp | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/cocos/base/ccUTF8.cpp b/cocos/base/ccUTF8.cpp index aad736e2ed..4ac099fbfa 100644 --- a/cocos/base/ccUTF8.cpp +++ b/cocos/base/ccUTF8.cpp @@ -183,8 +183,6 @@ long getCharacterCountInUTF8String(const std::string& utf8) return getUTF8StringLength((const UTF8*)utf8.c_str()); } - - } //namespace StringUtils { diff --git a/cocos/network/HttpClient-android.cpp b/cocos/network/HttpClient-android.cpp index 28852fd614..4a813af923 100644 --- a/cocos/network/HttpClient-android.cpp +++ b/cocos/network/HttpClient-android.cpp @@ -585,9 +585,9 @@ private: std::string strValue = ""; if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, jstr, strValue)) { - ret = strdup(strValue.c_str()); + strValue = ""; } - + ret = strdup(strValue.c_str()); return ret; }