我正在尝试使用node.js和socket.io为android应用程序和桌面浏览器创建一个社交应用程序。
在浏览器客户端,一切都很好,问题是android客户端,实际上我用的是socket.io java客户端,我希望用原生c++来实现,我用jni成功调用了socket.io java客户端类,它成功地连接到node.js服务器。两个发射器回调客户端从服务器接收从桌面浏览器javascript客户端发送的消息。
当我添加一行
给服务器的消息env->CallObjectMethod(globalSocketObj,emit,lo,o);
…应用程序崩溃。
-崩溃消息-
11-21 10:10:58.417 9310-9310/com.example.nyari.advancenative
e/dalvikvm:jni错误(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.advancentactive a/libc:
致命信号6(sigabrt),位于0x0000245E(代码=-6),线程9310
(i.有利)11-21 10:10:58.418
9310-9310/com.example.nyari.advancenative a/libc:发送停止信号至
PID:9310在void debuggerd_signal_handler中(in t,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();
}
}
});
}
};
}
}
最佳答案
你在这里做的有几个问题:
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)
创建的对象创建全局引用,这意味着一旦从当前方法返回,就无法再引用该对象。关于android - Android JNI APP从另一个 native 方法调用CallObjectMethod崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40718508/