Java android中所有活动的单socket.IO连接

Java android中所有活动的单socket.IO连接,java,android,android-activity,interface,socket.io,Java,Android,Android Activity,Interface,Socket.io,我已经为SocketIOClient引用创建了Singleton类。服务器已连接。我可以从activity向SocketIOClient发送请求。但我怎样才能从活动中的单例类得到响应呢 以下是我的活动: import java.net.MalformedURLException; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.os.Bun

我已经为SocketIOClient引用创建了Singleton类。服务器已连接。我可以从activity向SocketIOClient发送请求。但我怎样才能从活动中的单例类得到响应呢

以下是我的活动:

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();
            }

        }
    });

}

} 
我的Singleton类还有on()方法:


在这里,Singleton类可以从服务器获得响应。但是我想知道,如何在我的活动中获得响应?

创建这样一个抽象类

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);

    }
}
在你的活动中 在调用套接字类时,也将ResponseHadler作为参数传递 例如:

在你的socket类中

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();
        }
    }
}
}

在你的活动中创建一个回调方法,这样你就好像需要向singleton类注册一个lister,一旦类获取了数据,它就会调用你注册的方法。谢谢你的回复。我是java的新手。你能给我一个实施的例子吗。
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();
        }
    }
}
}