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

时间:2018-01-30 12:11:33

标签: android python sockets android-studio socket.io

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

这是代码:

        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

1 个答案:

答案 0 :(得分:0)

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.