【问题标题】:Android handler bufferedreader.read() closes the app when recieving dataAndroid handler bufferedreader.read() 接收数据时关闭应用
【发布时间】:2018-01-30 12:11:32
【问题描述】:

嗨,我是 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

【问题讨论】:

  • when i use .readline() my textview only updates when i close the program 。那么比服务器不会有发送一条线。只有一些字符。服务器也应该发送线路。一行以“\n”字符结束。
  • bufferedReader.close(); 哦哦...把它放在while循环之后。从已经关闭的流中读取它很困难;-)。
  • 对不起先生,我的问题不是很清楚,当我关闭客户端程序时,文本视图只更新,我要编辑我的问题
  • 哦好的先生哈哈
  • 先生可以删除 bufferedReader.close();

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


【解决方案1】:

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.

【讨论】:

  • 我应该从一开始就这样做,非常感谢先生:)
猜你喜欢
  • 2020-11-13
  • 1970-01-01
  • 1970-01-01
  • 2017-05-21
  • 2019-01-01
  • 2016-12-18
  • 2023-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多