我有一个包含两部分的代码:Java 和 JNI-cpp。我有两个单独的 cpp 文件,每个文件都有一种方法。首先我调用 Session.cpp,然后调用 Camera.cpp。
这两个参数在 cpp 方法中是相同的。
您可以在下面看到 Java 部分:
// 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
当我调用 signIn 方法时,一切正常。signIn 方法编写如下:
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");
}
}
当我调用 getCameraDictionary 方法时,它在第一行失败。您可以在下面找到代码:
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;
}
当我调试时,我发现原因是sessionId 和 serviceIp 作为 NULL 传递。该方法出现的问题是GetStringUTFChars(sessionId, NULL)
。此方法不接受 NULL 值作为第一个参数。但是在 signIn 方法中一切正常。可能是什么问题?
我尝试了很多事情,包括内存管理问题或复制变量。