是什么导致我的Android应用程序通过插座连接到笔记本电脑后关闭?
我正在通过 android studio 制作一个 android 应用程序,在屏幕上显示加速度计数据,我尝试通过套接字将其发送到我的笔记本电脑,但该应用程序在将此数据发送到我的笔记本电脑一次后自行关闭。
这是套接字类的代码示例
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;
}}
这是 mainActivity 代码:
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) {
}}
我的笔记本电脑充当带有 python 脚本的服务器,其代码如下:
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()
这是我在 python 控制台上遇到的错误:
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 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 AsyncTask 实例,您只能调用一次
execute()
。不要使用全局 AsyncTask 实例,但为每条消息创建一个新的连接,
然后使用两个异步任务,另一个用于发送数据。
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.