Single socket.IO connection for all activities in android
android, android-activity, interface, java, socket.io
Solution
Create an abstract class like this
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);
}
}
Inside your activity While calling socket class, pass the ResponseHadler also as a parameter Example:
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
}
}
And inside your 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();
}
}
}
}
Problem
I have created Singleton class for SocketIOClient reference by here. Server was connected. I can able to send request from activity to SocketIOClient. But how can I get response from Singleton class in Activity? Here My 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(); } } }); } } ``` Also My Singleton Class have on() method: ``` @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(); } } } ``` Here Singleton class can get response from server. But I want to know,how to get the response in my activity?