Android JNI APP在另一个本地方法上调用CallObjectMethod

时间:2016-11-21 10:59:23

标签: android java-native-interface

我想使用Node.js和socket.io为Android应用和桌面浏览器创建一个社交应用。

在浏览器客户端上,每个东西都运行良好,问题是android客户端。实际上我使用了socket.io java客户端,我希望在本机c ++中这样做。我已成功调用了socket.io java客户端类使用jni,它成功连接到我的node.js服务器。两个发射器回调客户端从桌面浏览器javascript客户端发送来自服务器的消息。

当我添加此行发送时,Android客户端向服务器发送消息时出现问题 消息到服务器

env->CallObjectMethod(globalSocketObj,emit,lo,o);

...应用程序崩溃了。 -CRASH MESSAGE -

  

11-21 10:10:58.417 9310-9310 / com.example.nyari.advancenative   E / dalvikvm:JNI ERROR(app bug):尝试使用过时的本地引用   0x1df00025 11-21 10:10:58.417   9310-9310 / com.example.nyari.advancenative E / dalvikvm:VM中止   11-21 10:10:58.418 9310-9310 / com.example.nyari.advancenative A / libc:   致命信号6(SIGABRT)位于0x0000245e(代码= -6),线程9310   (i.advancenative)11-21 10:10:58.418   9310-9310 / com.example.nyari.advancenative A / libc:发送停止信号   pid:9310 in void debuggerd_signal_handler(int,siginfo_t *,void *)

必须在本机代码中完成这个东西已经花了我三天。谢谢你的帮助。

本地代码:

//METHOD CONNECTING WITH NODE SERVER
JNIEXPORT void JNICALL Java_com_example_nyari_advancenative_MainActivity_count
                (JNIEnv* env, jobject obj){
            //LOCAL VARIABLES TO INSTANCES/////////////////////////////////////////////////
            Main_class=env->GetObjectClass(obj);//GETTING MAINACTIVITY OBJECT
            jstring f=env->NewStringUTF("http://192.168.43.113:8081");
            jstring il=env->NewStringUTF("hello");
            jstring ili=env->NewStringUTF("message");
        jclass my_socket=env->FindClass("io/socket/client/Socket");//GETTING SOCKET CLASS OF SOCKETIO
        jclass my_IO=env->FindClass("io/socket/client/IO");//GETTING IO LOCAL CLASS
            jobject socketObj=env->AllocObject(my_socket);//socketIO object
           jobject my_IOobj=env->AllocObject(my_IO);//GETTING IO OBJECT FROM IO CLASS
            //GETTING SOCKET STATIC METHOD FROM IO
           jmethodID static_socket=env->GetStaticMethodID(my_IO,"socket","(Ljava/lang/String;)Lio/socket/client/Socket;");
            //GLOBAL VARIABLES INSTANCES
            globalSocket= (jclass) env->NewGlobalRef(my_socket);//GLOBAL REFERENCE OF SOCKET CLASS
            globalSocketObj=env->NewGlobalRef(socketObj);//GLOBAL SOCKETIO REFERENCE OBJECT
        ///
            //INSTANTIATING GLOBAL REFERENCE OF SOCKET OBJECT
        globalSocketObj=env->CallObjectMethod(my_IOobj, static_socket, f);
            //LOCAL REFERENCES VARIABLES
            //GETTING CONNECT METHOD OF SOCKET CLASS
            jmethodID socket_connect=env->GetMethodID(my_socket,"connect","()Lio/socket/client/Socket;");
            //GETTING THE RECEIVE EMMITER CLASS FROM MAINACTIVITY
            receiveField=env->GetFieldID(Main_class,"receive","Lio/socket/emitter/Emitter$Listener;");
            //GETTING OBJECT FROM RECEIVEFIELD EMITTER FROM IN MAINACTIVITY
            receiveObj=env->GetObjectField(obj,receiveField);
            ///METHOD FROM SOCKET CLASS on FOR HELLO MESSAGE WITH SERVER
            onReceive=env->GetMethodID(my_socket,"on","(Ljava/lang/String;Lio/socket/emitter/Emitter$Listener;)Lio/socket/emitter/Emitter;");
            /////////////////
            //GETTING CHAT EMITTER FIELD FROM  MAINACTIVITY
            receiveMessage=env->GetFieldID(Main_class,"chats","Lio/socket/emitter/Emitter$Listener;");
            //GETTING OBJECT FROM CHATFIELD EMITTER FROM IN MAINACTIVITY
            receiveMessageObj=env->GetObjectField(obj,receiveMessage);
            ///METHOD FROM SOCKET CLASS on FOR MESSAGES SENT AND RECEIVED
            onReceiveMessage=env->GetMethodID(my_socket,"on","(Ljava/lang/String;Lio/socket/emitter/Emitter$Listener;)Lio/socket/emitter/Emitter;");
        ////////////////////////////
            env->CallObjectMethod(globalSocketObj,onReceive,il,receiveObj);
            env->CallObjectMethod(globalSocketObj,onReceiveMessage,ili,receiveMessageObj);
        env->CallObjectMethod(globalSocketObj,socket_connect);

          }

    ///PROBLEMATIC METHOD USED TO SEND MESSAGE TO SERVER
JNIEXPORT void JNICALL Java_com_example_nyari_advancenative_MainActivity_vari(JNIEnv* env, jobject obj){
        jstring il=env->NewStringUTF("hello");//message to be sent to the server
        jstring ili=env->NewStringUTF("message");//for callback method in server
        jclass ob=env->GetObjectClass(globalSocketObj);
        jmethodID emit=env->GetMethodID(ob,"emit","(Ljava/lang/String;[Ljava/lang/Object;)Lio/socket/emitter/Emitter;");
        env->CallObjectMethod(globalSocketObj,emit,ili,il);//PROBLEMATIC CALL

    }

JAVA CODE :


    public class MainActivity extends AppCompatActivity {
    static{
    /*try{
        System.loadLibrary("louts");
    }catch(Error | Exception ignore){

    }*/
        System.loadLibrary("louts");
    }
        TextView text,tex;

        public native void connect();
        public native void count();
        public native void vari();
        public native Socket network();
    public native void send();
        String message;
    Emitter.Listener receive,chats;
    String jo= io.socket.client.Socket.EVENT_CONNECT;
        ArrayList<String> items;
        ArrayAdapter<String> adapter;
        ListView list;
        EditText texter;
        Button button;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);
            text=(TextView)findViewById(R.id.numb);
            tex=(TextView)findViewById(R.id.num);
    //LISTVIEW TO ADD MESSAGES FROM NODE SERVER
            items=new ArrayList<String>();
         adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,items);

            list=(ListView)findViewById(R.id.list);
            list.setAdapter(adapter);
    //EDITTEXT TO GET MESSAGES
            texter=(EditText)findViewById(R.id.texter);
    //CONTAINS THE EMITTER CALLBACKS METHODS TO RECEIVE MESSAGES FROM NODE SERVER
            emit();
    //THREAD THAT CONTAINS  NATIVE METHOD count() that connects to NODE SERVER
    Thread y=new Thread(new Runnable(){

        @Override
        public void run() {
            count();
        }
    });
            y.start();
    //BUTTON THAT MESSAGE TO NODE SERVER
            button=(Button)findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String j=null;
                    j=texter.getText().toString();
                    JSONObject reg2=new JSONObject();
                    try {
                        reg2.put("ki",j);
                        /*socket.emit("message",reg2);*/
                        vari();
                        texter.setText("");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });

        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            connect();
        }

        private void emit(){
            receive= new Emitter.Listener() {
                @Override
                public void call(Object... args) {//CALLBACK METHOD FOR HELLO MESSAGE FROM SERVER
                    final JSONObject obj = (JSONObject)args[0];
                    MainActivity.this.runOnUiThread(new Runnable(){

                        @Override
                        public void run() {
                            try {
                                message=obj.getString("ki");
                                text.setText(message);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });

                }
            };
            chats= new Emitter.Listener() {
                @Override
                public void call(Object... args) {//CALLBACK METHOD TO RECEIVE CHAT MESSAGES FROM SERVER
                    final JSONObject obj = (JSONObject)args[0];
                    MainActivity.this.runOnUiThread(new Runnable(){

                        @Override
                        public void run() {
                            try {
                                message=obj.getString("mess");
                              //  items.add(message);
                                adapter.add(message);
                                adapter.notifyDataSetChanged();
                                //tex.setText(message);
                                //list.setVerticalScrollbarPosition(list.getHeight());
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            };

        }
    }

2 个答案:

答案 0 :(得分:1)

你在这里做的有几个问题:

       globalSocketObj=env->NewGlobalRef(socketObj);//GLOBAL SOCKETIO REFERENCE OBJECT
    ///
        //INSTANTIATING GLOBAL REFERENCE OF SOCKET OBJECT
    globalSocketObj=env->CallObjectMethod(my_IOobj, static_socket, f);

首先,您要创建Socket引用的socketObj实例的全局引用。创建全局引用的关键是,与本地引用不同,当您返回Java时,它不会被删除。到目前为止这么好,假设你想让Socket实例不被垃圾收集。

第一个问题是你在下一行直接覆盖globalSocketObj的值,因此你刚刚创建的全局引用现在处于不确定状态。这意味着,一旦从当前方法返回,您就不能再引用该Socket对象,这种方法首先会破坏创建全局引用的目的。由于您无法删除您创建的全局引用,因此您只是为自己赢得了内存泄漏。

第二个潜在的问题是,您没有为使用env->CallObjectMethod(my_IOobj, static_socket, f)创建的对象创建全局引用,这意味着您无法再次引用该对象你从当前的方法中退回了。

答案 1 :(得分:1)

我终于找到了解决方案。我首先要感谢Micheal因为你的答案 实际上睁开了我的眼睛,让我以更清洁的方式从本机代码连接。实际上你在哪里但有很多内存泄漏和应用程序崩溃OutOfMemryError所以我做了以下方式 首先在我的cpp文件中创建一个返回IO.socket()对象的jobject,然后我实例化 回到我的java文件中,使用常规套接字。这是我的原生代码:

JNIEXPORT jobject JNICALL Java_com_example_nyari_advancenative_MainActivity_SocketIO(JNIEnv * env,jclass clazz){         jstring f = env-&gt; NewStringUTF(“http://192.168.43.113:8081”);         jclass my_IO = env-&gt; FindClass(“io / socket / client / IO”); //获取IO本地类         jobject my_IOobj = env-&gt; AllocObject(my_IO); //从IO类获取IO对象         //从IO获取SOCKET静态方法         jmethodID static_socket = env-&gt; GetStaticMethodID(my_IO,“socket”,“(Ljava / lang / String;)Lio / socket / client / Socket;”);     return env-&gt; CallObjectMethod(my_IOobj,static_socket,f);

}

Java代码:

   public class MainActivity extends AppCompatActivity {
    static{
        System.loadLibrary("louts");
    }
        TextView text,tex;

        public native void connect();
        public static native Socket SocketIO();
        Socket sok;
        String message;
    Emitter.Listener receive,chats;
    String jo= io.socket.client.Socket.EVENT_CONNECT;
        ArrayList<String> items;
        ArrayAdapter<String> adapter;
        ListView list;
        EditText texter;
        Button button;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);
            text=(TextView)findViewById(R.id.numb);
            tex=(TextView)findViewById(R.id.num);
    //LISTVIEW TO ADD MESSAGES FROM NODE SERVER
            items=new ArrayList<String>();
         adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,items);

            list=(ListView)findViewById(R.id.list);
            list.setAdapter(adapter);
    //EDITTEXT TO GET MESSAGES
            texter=(EditText)findViewById(R.id.texter);
    //CONTAINS THE EMITTER CALLBACKS METHODS TO RECEIVE MESSAGES FROM NODE SERVER
            emit();
    //THREAD THAT CONTAINS  NATIVE METHOD count() that connects to NODE SERVER
    Thread y=new Thread(new Runnable(){

        @Override
        public void run() {
            sok=SocketIO();
            sok.on("hello", receive);
            sok.on("message",chats);
            sok.connect();
        }
    });
            y.start();
    //BUTTON THAT MESSAGE TO NODE SERVER
            button=(Button)findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String j=null;
                    j=texter.getText().toString();
                    JSONObject reg2=new JSONObject();
                    try {
                        reg2.put("ki",j);
                        sok.emit("message",reg2);
                        texter.setText("");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });

        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            connect();
        }

        private void emit(){
            receive= new Emitter.Listener() {
                @Override
                public void call(Object... args) {//CALLBACK METHOD FOR HELLO MESSAGE FROM SERVER
                    final JSONObject obj = (JSONObject)args[0];
                    MainActivity.this.runOnUiThread(new Runnable(){

                        @Override
                        public void run() {
                            try {
                                message=obj.getString("ki");
                                text.setText(message);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });

                }
            };
            chats= new Emitter.Listener() {
                @Override
                public void call(Object... args) {//CALLBACK METHOD TO RECEIVE CHAT MESSAGES FROM SERVER
                    final JSONObject obj = (JSONObject)args[0];
                    MainActivity.this.runOnUiThread(new Runnable(){

                        @Override
                        public void run() {
                            try {
                                message=obj.getString("mess");
                              //  items.add(message);
                                adapter.add(message);
                                adapter.notifyDataSetChanged();
                                //tex.setText(message);
                                //list.setVerticalScrollbarPosition(list.getHeight());
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            };

        }
    }
Hope it helps some one with the same problem