jstring参数在JNI中作为nil传递

时间:2016-07-21 12:53:28

标签: java android c++ java-native-interface android-studio-2.2

我有一个包含两部分的代码:Java和JNI-cpp。我有两个单独的cpp文件,每个文件都有一个方法。首先,我调用Session.cpp,然后调用Camera.cpp。

cpp方法中的两个参数相同。

您可以在下面看到Java部分:

$scope.workoutTarget

当我调用signIn方法时,一切正常。 signIn方法编写如下:

// Sign in
    String
            username = ServiceModel.getInstance().getModelUsername(),
            password = ServiceModel.getInstance().getModelPassword(),
            licenceKey = ServiceModel.getInstance().getModelLicenceKey(),
            serviceIp = ServiceModel.getInstance().getModelCurrentServiceIp(),
            hashedPassword = md5(password);
    // Get session id
    String sessionId
            = signIn(username,
            hashedPassword,
            licenceKey,
            serviceIp); // Session Manager
    ServiceModel.getInstance().setModelSessionId(sessionId);
    // Get camera info
    HashMap<String, String> cameraDictionary
            = getCameraDictionary(sessionId,
            serviceIp); // Camera Manager

当我调用getCameraDictionary方法时,它在第一行失败。您可以在下面找到以下代码:

extern "C"
jstring
Java_com_company_mobile_StreamActivity_signIn(
    JNIEnv *env,
    jobject,
    jstring username,
    jstring password,
    jstring licenceKey,
    jstring serviceIp) {
std::string strServiceIp(env->GetStringUTFChars(serviceIp, NULL));
std::string serviceUrl = "http://" + strServiceIp + "/SessionManager";
SessionManager::IVSOSSessionManagerProxy proxy(serviceUrl.c_str());
std::string strUsername(env->GetStringUTFChars(username, NULL));
std::string strPassword(env->GetStringUTFChars(password, NULL));
std::string strLicenceKey(env->GetStringUTFChars(licenceKey, NULL));
std::string sId;
std::string &sessionId = sId;
if (proxy.signIn(strUsername,
strPassword,
strLicenceKey,
sessionId) == SOAP_OK) {
    return env->NewStringUTF(sessionId.c_str());
} else {
    return env->NewStringUTF("NO");
}
}

在我调试时,我发现原因是 sessionId和serviceIp传递为NULL 。该方法出现的问题是extern "C" std::unordered_map<std::string, std::string> Java_com_company_mobile_StreamActivity_getCameraDictionary( JNIEnv *env, jobject, jstring sessionId, jstring serviceIp) { const char* serIp = env->GetStringUTFChars(sessionId, NULL); std::string serviceUrl = "http://" + std::string(serIp) + "/EncoderManager"; std::string strSessionId(env->GetStringUTFChars(serviceIp, NULL)); CameraManager::ns__IVSOSEncoderContainerSOAPRef cc; CameraManager::ns__IVSOSEncoderContainerSOAPRef &cameraContainer = cc; CameraManager::IVSOSEncoderManagerProxy proxy(serviceUrl.c_str()); proxy.getAllCamera(strSessionId, cameraContainer); std::string cameraName; std::string cameraUrl; std::string cameraUri; std::string cameraUsername; std::string cameraPassword; std::string rtspUrl; std::unordered_map <std::string, std::string> cameraDictionary; if (cameraContainer.mObject.size() > 0) { for (int i = 0; i < cameraContainer.mObject.size(); i++) { cameraName = cameraContainer.mObject[i].mEncoderName; cameraUrl = cameraContainer.mObject[i].mEncoderIp; cameraUri = cameraContainer.mObject[i].mEncoderSecondaryRtsp; cameraUsername = cameraContainer.mObject[i].mEncoderUsername; cameraPassword = cameraContainer.mObject[i].mEncoderPassword; if (!(cameraUsername.size() > 0)) { rtspUrl = "rtsp://" + cameraUsername + ":" + cameraPassword + "@" + cameraUrl + cameraUri; } else { rtspUrl = "rtsp://" + cameraUrl + cameraUri; } cameraDictionary[cameraName] = rtspUrl; } } return cameraDictionary; } 。此方法不接受NULL值作为第一个参数。但是在signIn方法中一切正常。可能是什么问题?

我尝试了很多东西,包括内存管理问题或复制变量。

0 个答案:

没有答案