Android 处理程序 bufferedreader.read() 在接收数据时关闭应用程序

标签 android python sockets android-studio socket.io

嗨,我是 android 编程的新手,在套接字中我试图从客户端接收数据,当我使用 .readline() 时,我的 TextView 仅在我关闭 python 中的客户端程序时更新,所以我使用 .read() 应用程序关闭可能是什么问题?

这是代码:

        class Mythread implements Runnable{



    Handler h = new Handler();

    @Override
    public void run() {

        Socket s;
        ServerSocket ss;
        InputStreamReader ist;
        BufferedReader bufferedReader;
        try {
            ss = new ServerSocket(21600);
            s = ss.accept();
            ist = new InputStreamReader(s.getInputStream());
            bufferedReader = new BufferedReader(ist);
            while (true){

                final String message = bufferedReader.readLine();
                Log.d("message", "message: " + message);

                h.post(new Runnable() {
                    @Override
                    public void run() {
                        ballscollect.setText(message);

                    }
                });
            }
        }catch (IOException e)
        {
            e.printStackTrace();
        }

    }
}

这是我的 python 客户端代码,每次单击开关时,它都会向应用程序发送输出字符串:

 import socket
from nanpy import (ArduinoApi, SerialManager)
import time
import thread

import Adafruit_CharLCD as LCD
import math

lcd_rs        = 26  
lcd_en        = 19
lcd_d4        = 13
lcd_d5        = 6
lcd_d6        = 5
lcd_d7        = 11
lcd_backlight = 4

lcd_columns = 16
lcd_rows    = 2

lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight)

lcd.message ('Balls Collected')

ledpin = 13
buttonpin = 12
buttonpin1 = 2
buttonpin2 = 3
buttonstate1 = 1
buttonstate2 = 1
buttonstate3 = 1

connection = SerialManager()
a = ArduinoApi(connection = connection)

a.pinMode(ledpin, a.OUTPUT)
a.pinMode(buttonpin, a.INPUT)
a.pinMode(buttonpin1, a.INPUT)
a.pinMode(buttonpin2, a.INPUT)

host = '192.168.1.7'
port = 21600
sock = socket.socket()
sock.connect((host,port))



def count():
    add = 0
    while True:


        buttonstate1 = a.digitalRead(buttonpin)
        buttonstate2 = a.digitalRead(buttonpin1)
        buttonstate3 = a.digitalRead(buttonpin2)

        if buttonstate1:

            a.digitalWrite(ledpin, a.HIGH)

        else:
            a.digitalWrite(ledpin, a.LOW)
            add += 1
            s = str(add)
            lcd.message ('\n'+s)
            sock.send(s)              #this is the output string that is sent to the app
            print ('sent')
            time.sleep (.5)

        if buttonstate2:

            a.digitalWrite(ledpin, a.HIGH)

        else:
            a.digitalWrite(ledpin, a.LOW)
            add += 1
            s = str(add)
            sock.send(s)       #this is the output string that is sent to the app
            lcd.message ('\n'+s)
            print ('sent')
            time.sleep (.5)


        if buttonstate3:
            a.digitalWrite(ledpin, a.HIGH)


        else:
            a.digitalWrite(ledpin, a.LOW)
            add += 1
            s = str(add)
            lcd.message ('\n'+s)
            sock.send(s)   #this is the output string that is sent to the app
            print ('sent')
            time.sleep (.5)                                             

    sock.close()


thread.start_new_thread(count, ())

我在 logcat 上遇到错误

01-30 22:39:23.437 21785-21802/com.example.mahilum.tbot D/message: message: null
01-30 22:39:23.437 21785-21802/com.example.mahilum.tbot D/message: message: null
01-30 22:39:23.437 21785-21802/com.example.mahilum.tbot D/message: message: null
01-30 22:39:23.438 21785-21802/com.example.mahilum.tbot D/message: message: null
01-30 22:39:23.438 21785-21802/com.example.mahilum.tbot D/message: message: null
01-30 22:39:23.438 21785-21802/com.example.mahilum.tbot D/message: message: null
01-30 22:39:23.438 21785-21802/com.example.mahilum.tbot D/message: message: null

最佳答案

sock.send(s) 更改为 sock.send(s + "\n")

记住我的第一条评论:

Well than the server will not have send a line. 
Only some characters. 
The server should send lines too. A line ends with a "\n" character.

关于Android 处理程序 bufferedreader.read() 在接收数据时关闭应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48521184/

相关文章:

android - 摩托罗拉设备 : org. threeten.bp.DateTimeException 解析 ThreeTen 中的日期时

android - 获取stringXmlContent并写入然后保存到存储在android中sdcard中的.xml文件中怎么可能?

php - 如何在php中格式化打印输出

java - 从池中消除突然关闭的套接字,Java

android - Phonegap android无法使用fileTransfer上传图像

android - 如何为 Cordova Ios/Windows Phone/Android 正确设置 "content-security-policy"?

python - 将方法映射到不同对象的列表

python - 在python中分组交易描述和计数

python - 如何在Python中创建 'word stream'和 'document stream'?

C - 连接函数 - 无效参数错误