diff --git a/cocos/storage/local-storage/LocalStorage-android.cpp b/cocos/storage/local-storage/LocalStorage-android.cpp index 93ea42ec15..67ebc8240e 100644 --- a/cocos/storage/local-storage/LocalStorage-android.cpp +++ b/cocos/storage/local-storage/LocalStorage-android.cpp @@ -111,21 +111,25 @@ void localStorageSetItem( const std::string& key, const std::string& value) } /** gets an item from the LS */ -std::string localStorageGetItem( const std::string& key ) +bool localStorageGetItem( const std::string& key, std::string *outItem ) { assert( _initialized ); JniMethodInfo t; - std::string ret; - if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "getItem", "(Ljava/lang/String;)Ljava/lang/String;")) { + if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "getItem", "(Ljava/lang/String;)Ljava/lang/String;")) + { jstring jkey = t.env->NewStringUTF(key.c_str()); jstring jret = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, jkey); - ret = JniHelper::jstring2string(jret); + outItem->assign(JniHelper::jstring2string(jret)); t.env->DeleteLocalRef(jret); t.env->DeleteLocalRef(jkey); t.env->DeleteLocalRef(t.classID); + return true; + } + else + { + return false; } - return ret; } /** removes an item from the LS */ diff --git a/cocos/storage/local-storage/LocalStorage.cpp b/cocos/storage/local-storage/LocalStorage.cpp index f91a134a7d..4580015617 100644 --- a/cocos/storage/local-storage/LocalStorage.cpp +++ b/cocos/storage/local-storage/LocalStorage.cpp @@ -120,23 +120,26 @@ void localStorageSetItem( const std::string& key, const std::string& value) } /** gets an item from the LS */ -std::string localStorageGetItem( const std::string& key ) +bool localStorageGetItem( const std::string& key, std::string *outItem ) { assert( _initialized ); - std::string ret; int ok = sqlite3_reset(_stmt_select); ok |= sqlite3_bind_text(_stmt_select, 1, key.c_str(), -1, SQLITE_TRANSIENT); ok |= sqlite3_step(_stmt_select); const unsigned char *text = sqlite3_column_text(_stmt_select, 0); - if (text) - ret = (const char*)text; - if( ok != SQLITE_OK && ok != SQLITE_DONE && ok != SQLITE_ROW) + if( (ok != SQLITE_OK && ok != SQLITE_DONE && ok != SQLITE_ROW) || !text) + { printf("Error in localStorage.getItem()\n"); - - return ret; + return false; + } + else + { + outItem->assign((const char*)text); + return true; + } } /** removes an item from the LS */ diff --git a/cocos/storage/local-storage/LocalStorage.h b/cocos/storage/local-storage/LocalStorage.h index c33a899b20..642451a221 100644 --- a/cocos/storage/local-storage/LocalStorage.h +++ b/cocos/storage/local-storage/LocalStorage.h @@ -46,7 +46,7 @@ void CC_DLL localStorageFree(); void CC_DLL localStorageSetItem( const std::string& key, const std::string& value); /** Gets an item from the JS. */ -std::string CC_DLL localStorageGetItem( const std::string& key ); +bool CC_DLL localStorageGetItem( const std::string& key, std::string *outItem ); /** Removes an item from the JS. */ void CC_DLL localStorageRemoveItem( const std::string& key );