'What is causing my android app to close after connecting with my laptop via sockets?
I'm making an android app via android studio that displays accelerometer data on the screen and I tried to send this to my laptop via sockets, but the app closes by itself after sending this data once to my laptop.
here's a code sample of the socket class
public class MessageSender extends AsyncTask<String, Void, Void> {
Socket sock;
DataOutputStream dos;
PrintWriter pw;
@Override
protected Void doInBackground(String... voids) {
String message = voids[0];
try
{
sock = new Socket("192.168.0.105", 8400);
pw = new PrintWriter(sock.getOutputStream());
pw.write(message);
pw.flush();
}catch (IOException ioe){
ioe.printStackTrace();
}
return null;
}}
Here's the mainActivity code:
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private static final String TAG = "MainActivity";
private SensorManager sensorManager;
Sensor accelerometer;
MessageSender messenger = new MessageSender();
TextView xValue, yValue, zValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xValue = (TextView) findViewById(R.id.xValue);
yValue = (TextView) findViewById(R.id.yValue);
zValue = (TextView) findViewById(R.id.zValue);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
@SuppressLint("SetTextI18n")
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
Log.d(TAG, "onSensorChanged: X" + sensorEvent.values[0] + " Y: " +
sensorEvent.values[1] + " Z: " + sensorEvent.values[2]);
xValue.setText("X-axis: " + sensorEvent.values[0]);
yValue.setText("Y-axis: " + sensorEvent.values[1]);
zValue.setText("Z-axis: " + sensorEvent.values[2]);
messenger.execute("{\"X\":"+sensorEvent.values[0]+", \"Y\":"+sensorEvent.values[1]+", \"Z\":"+sensorEvent.values[2]+"}");
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}}
My laptop acts as the server with a python script, the code for which is below:
import socket
import json
TCP_IP = '192.168.0.105'
TCP_PORT = 8400
BUFFER_SIZE = 1024 # Normally 1024, but we want fast response
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
conn, addr = s.accept()
print('Connection address:', addr)
while 1:
data = conn.recv(BUFFER_SIZE)
parseData = json.loads(data)
if not data: break
print("received data:")
print(parseData)
conn.send(data) # echo
conn.close()
this is the error I'm getting on my python console:
received data:
{'X': -0.25576973, 'Y': -0.12328009, 'Z': 9.587313}
Traceback (most recent call last):
File "C:/Users/theva/Desktop/server.py", line 15, in <module>
data = conn.recv(BUFFER_SIZE)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
I'm trying to get the app to continuously send data to my pc as long as the connection is maintained else just display that data on the mobile screen I'm probably doing this the wrong way but please help me with this, I don't mind criticism but just say it in simple layman terms please.
Thanks in advance!
Solution 1:[1]
messenger.execute("{"X":"+sensorEvent.values[0]+....
You can only call execute() once for an AsyncTask instance.
Dont use a global AsyncTask instance but create a new one for every message.
And if you want a permanent connection then use two async tasks. One to establish the connection. The other to send data.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | blackapps |
