java 如何将jobject转换为jstring
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14036004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert jobject to jstring
提问by glo
I am trying to get a string in return to a function call from cpp to java.
我正在尝试获取一个字符串以返回从 cpp 到 java 的函数调用。
This is my JNI call
这是我的 JNI 电话
string GetIDJni()
{
cocos2d::JniMethodInfo methodInfo;
if (! JniHelper::getStaticMethodInfo(methodInfo, CLASS_NAME, "GetID", "()Ljava/lang/String"))
{
return "";
}
jobject retObj = methodInfo.env->CallStaticObjectMethod(methodInfo.classID, methodInfo.methodID);
jstring retStr = (jstring)retObj;
methodInfo.env->DeleteLocalRef(methodInfo.classID);
return (JniHelper::jstring2string(retStr));
}
On compiling i get the error
编译时出现错误
error: invalid conversion from '_jobject*' to '_jstring*'
错误:从“_jobject*”到“_jstring*”的无效转换
Can anyone please tell me how to solve this problem.
谁能告诉我如何解决这个问题。
回答by user1169079
Here you go ...
干得好 ...
const char* GetIDJni() {
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "GetIDJni", "()Ljava/lang/String;")) {
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
CCString *ret = new CCString(JniHelper::jstring2string(str).c_str());
ret->autorelease();
t.env->DeleteLocalRef(str);
return ret->m_sString.c_str();
}
return 0;
}
And if you want to get it return as std::String then
如果你想让它作为 std::String 返回,那么
std::string GetIDJni() {
std::string ret;
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "GetIDJni", "()Ljava/lang/String;")) {
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
ret=JniHelper::jstring2string(str);
t.env->DeleteLocalRef(str);
return ret;
}
return 0;
}