我已为here创建了用于SocketIOClient引用的Singleton类。服务器已连接。我可以将活动请求发送到SocketIOClient。但是如何在Activity中获得Singleton类的响应?
我的活动:
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()方法:
@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();
}
}
}
这里Singleton类可以从服务器获得响应。但我想知道,如何在我的活动中得到回应?
答案 0 :(得分:2)
像这样创建一个抽象类
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);
}
}
在您的活动中 在调用socket类时,也将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();
}
}
}
}