gpt4 book ai didi

java - android中所有 Activity 的单一socket.IO连接

转载 作者:太空狗 更新时间:2023-10-29 13:23:46 25 4
gpt4 key购买 nike

我已经为 SocketIOClient 引用创建了 Singleton 类 here .服务器已连接。我可以将请求从 Activity 发送到 SocketIOClient。但是我怎样才能从 Activity 中的 Singleton 类得到响应呢?

这是我的 Activity :

import java.net.MalformedURLException;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {
EditText uname, passwd;
Button login;
JSONObject json;
SocketIOClient socket;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
socket = new SocketIOClient();
try {
SocketIOClient.initInstance();
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

json = new JSONObject();
uname = (EditText) findViewById(R.id.unameED);
passwd = (EditText) findViewById(R.id.passwdED);
login = (Button) findViewById(R.id.loginButton);
login.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

try {
json.put("username", uname.getText().toString().trim());
json.put("password", passwd.getText().toString().trim());
//request send to server
SocketIOClient.emit("login_request", json);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
});

}

}

我的单例类也有 on() 方法:

        @Override
public void on(String event, IOAcknowledge ack, Object... args) {
JSONArray jarr_args = new JSONArray();
JSONObject jobj_in = new JSONObject();
if (event.equals("registration_status")) {
jarr_args.put(args[0]);
try {
jobj_in = jarr_args.getJSONObject(0);
Log.d("Result", jobj_in.getString("result"));
if (jobj_in.getString("result").equals("success")) {

} else {
Log.d("check:", "username and password");

}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

这里单例类可以得到服务器的响应。但我想知道,如何在我的 Activity 中获得响应?

最佳答案

像这样创建一个抽象类

public abstract class ResponseHandler 
{
private Context context;

public abstract void execute (JSONObject jsonObject) throws JSONException;

public ResponseHandler (Context ctx)
{
this.context = ctx;
}

public void handleObject(JSONObject jsonObject) throws Exception
{
execute(jsonObject);

}
}

在你的 Activity 中在调用套接字类时,也将 ResponseHadler 作为参数传递示例:

SocketIOClient.initInstance(your parameters, new ResponseHandler(this)
{
//ResponseHandler have an abstract method called execute(). So you are overriding it here
@Override
public void execute(JSONObject jsonObject) throws JSONException
{
// Here you will get your JSONObject passed from socket class
}
}

在你的套接字类中

public class YourSocketClass
{
private ResponseHandler handler;

public static void initInstance(your parameter, ResponseHandler responseHandler)
{
this.handler = responseHandler;

// Do your operations here
}

@Override
public void on(String event, IOAcknowledge ack, Object... args)
{
JSONArray jarr_args = new JSONArray();
JSONObject jobj_in = new JSONObject();
if (event.equals("registration_status"))
{
jarr_args.put(args[0]);
try
{
jobj_in = jarr_args.getJSONObject(0);
Log.d("Result", jobj_in.getString("result"));
if (jobj_in.getString("result").equals("success"))
{
//If you want to pass your jsonobject from here to activity
//Do something like this
handler.handleObject(jobj_in);
}
else
{
Log.d("check:", "username and password");
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
}
}

关于java - android中所有 Activity 的单一socket.IO连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24632575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com