【问题标题】:calling Endpoints AsyncTask with JNI error使用 JNI 错误调用 Endpoints AsyncTask
【发布时间】:2023-03-26 21:49:01
【问题描述】:

我正在使用 Appengine 作为后端开发一款适用于 Android 的多人游戏。一切都设置得很好,然后我为 Android 生成了端点代码,它也非常好用。

执行 Endpoints 类的 Java 类。

java代码:

public static void saveData()
{
    new EndpointsTask().execute(mActivity.getApplicationContext());
}

public class EndpointsTask extends AsyncTask<Context, Integer, Long> {
    protected Long doInBackground(Context... contexts) {
        Noteendpoint.Builder endpointBuilder = new Noteendpoint.Builder(
            AndroidHttp.newCompatibleTransport(),
            new JacksonFactory(),
            new HttpRequestInitializer() {
                public void initialize(HttpRequest httpRequest) { }
            });
        Noteendpoint endpoint = CloudEndpointUtils.updateBuilder(
            endpointBuilder).build();
        try {
            Note note = new Note().setDescription("Note Description");
            //String noteID = new Date().toString();
            String noteID = "lalala";
            note.setId(noteID);

            note.setEmailAddress("E-Mail Address");      
            Note result = endpoint.insertNote(note).execute();
        }
    }
}

然后从 NDK NativeActivity c++ 代码中调用:

void CCHelper::saveData()
{
    PluginJniMethodInfo minfo;

    if(PluginJniHelper::getStaticMethodInfo(minfo, "com/util/Helper",  "saveData", "()V"))
    {
        minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID,NULL);
        minfo.env->DeleteLocalRef(minfo.classID);
    }
}

当我使用 Jni 从 C++ 调用它时,出现错误:

04-16 17:31:20.178: W/dalvikvm(28843): dvmFindClassByName 拒绝“com/util/Helper” 04-16 17:32:16.553: A/libc(28843): 致命信号 11 (SIGSEGV) 在 0x00000000 (code=1)

我正在使用基于 NativeActivity 的应用程序 (cocos2d-x)。

有解决办法吗?是否可以从 NativeActivity 调用 Endpoints 代码?

【问题讨论】:

    标签: android c++ google-app-engine android-ndk google-cloud-endpoints


    【解决方案1】:

    所以我更改了我的 C++ 实现以使用 std::thread 调用 JNI

    std::thread t (&HelloWorld::ThreadFunction,this);
    t.join();
    
    void HelloWorld::ThreadFunction()
    {
        CCHelper::saveData();
    }
    

    java代码,我去掉了AsyncTask执行:

    public static void saveData()
    {
        Noteendpoint.Builder endpointBuilder = new Noteendpoint.Builder(
            AndroidHttp.newCompatibleTransport(),
            new JacksonFactory(),
            new HttpRequestInitializer() {
                public void initialize(HttpRequest httpRequest) { }
        });
    
        Noteendpoint endpoint = CloudEndpointUtils.updateBuilder(
            endpointBuilder).build();
        try {
            Note note = new Note().setDescription("Note Description");
            String noteID = new Date().toString();
            note.setId(noteID);
    
            note.setEmailAddress("E-Mail Address");      
            Note result = endpoint.insertNote(note).execute();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    PluginJniHelper::getStaticMethodInfo JNI 代码内部

    ...
    if(!JAVAVM)
    {
        LOGD("JavaVM is NULL");
        break;
    }
    
    if (JAVAVM->GetEnv((void**)env, JNI_VERSION_1_6) != JNI_OK)
    {
        LOGD("Failed to get the environment using GetEnv()");
        break;
    }
    
    if (JAVAVM->AttachCurrentThread(env, 0) < 0)
    {
        LOGD("Failed to get the environment using AttachCurrentThread()");
        break;
    }
    ...
    

    我得到错误 Failed to get the environment using GetEnv() 这是 VM 在调用 GetEnv() 时失败。

    这里有什么我遗漏的吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      相关资源
      最近更新 更多