I created a Java client, which should connect to a python server. As a standalone it works perfectly fine, connection is established and data will be sending through.
public class ClientTest {
public static void main(String args[]) {
requestGazeData();
}
public static void requestGazeData() {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket request = context.socket(ZMQ.REQ);
request.connect("tcp://xxx.x.x.x:50020");
// Ask for the sub port
request.send("SUB_PORT");
String subPort = request.recvStr();
System.out.println("subPort: " + subPort);
// open a sub port to listen to pupil
ZMQ.Socket subscriber = context.socket(ZMQ.SUB);
subscriber.connect("tcp://xxx.x.x.x:" + subPort);
String gaze = "gaze";
subscriber.subscribe(gaze.getBytes(Charset.forName("UTF-8")));
while (true) {
String msg = subscriber.recvStr();
System.out.println(msg);
subscriber.close();
context.term();
}
}
}
Now this client has to run after the app started, but nothing happens. I don't get any data from the server and it get stuck. It is definitely a problem from the Android side, I reckon moreover especially the activity class. This one looks as follows:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set function for button
findViewById(R.id.button).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
ClientTest.requestGazeData();
}
});
}
}
I don't get an exception either. In my Manifest.xml I put all needed permissions:
Lastly, I allowed the connection on the ports in my firewall (via ufw in Ubuntu). I've run out of ideas how to fix it and make it work.
UPDATE:
Due to the one possible answer I tried the AsyncTask, but still anything happens. Here's my updated activity class:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set function for the button
findViewById(R.id.connectButton).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
new NetworkCall().execute("");
}
});
}
private class NetworkCall extends AsyncTask {
@Override
protected String doInBackground(String... params) {
ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket request = context.socket(ZMQ.REQ);
request.connect("tcp://xxx.x.x.x:50020");
System.out.println("Connected");
// ask for the sub port
request.send("SUB_PORT");
String subPort = request.recvStr();
System.out.println("subPort: " + subPort);
//int subPortNo = Integer.parseInt(subPort);
// open a sub port to listen to pupil
ZMQ.Socket subscriber = context.socket(ZMQ.SUB);
subscriber.connect("tcp://xxx.x.x.x:" + subPort);
String gaze = "gaze";
subscriber.subscribe(gaze.getBytes(Charset.forName("UTF-8")));
while (true) {
String msg = subscriber.recvStr();
System.out.println(msg);
//subscriber.close();
//context.term();
}
}
@Override
protected void onPostExecute(String result) {
System.out.println("DONE");
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
解决方案
So after trying literally everything, I disabled the firewall and finally the connection worked. Sometimes it can be the small details.