0

好的,所以我正在设计一个 Android MUD 客户端作为我学校项目的一部分。但是,在实现 ANSI 颜色解析时,我遇到了问题。我逐字节读取数据。我尝试将字符“十六进制”设置为“\033”、“27”和“0x1B”,但我似乎永远无法让它检测到转义字符。你们看我检查有什么问题吗?此外,行“char check = String.valueOf(j).charAt(0);” 是临时的,我最初试图检查字符变量“hex”与字节“j”。是否有更好的方法来检查角色?

                    while(isConnected) {

                    int j = 0;

                    try {
                            int i = arrayOfByte.length;
                            j = streamInput.read(arrayOfByte, 0, i);

                            char check = String.valueOf(j).charAt(0);

                            Log.d("Console","Char is - " + check);
                            if (j == -1)
                            {

                                Log.d("Console","j = -1");
                                throw new Exception("Error while reading socket.");
                            } else if (j == 0) {

                                Log.d("Console","Continuing");
                                continue;
                            }  else if (check == hex)  {
                                Log.d("Console","Yo, daddio!");
                            } else {

                                final String strData = new String(arrayOfByte, 0, j).replace("\r", "");


                                runOnUiThread(new Runnable() {
                                     public void run() {


                                         textContent.append(strData);
                                         scrollToBottom();

                                    }
                                });

                            }
                    } catch (Exception e) {

                        Handler handlerException = GameWindow.this.mHandler;
                        String strException = e.getMessage();
                        final String strMessage = "Error while receiving from server:\r\nConnection terminated";

                        Runnable rExceptionThread = new Runnable()
                        {
                            public void run()
                            {
                                Toast.makeText(context, strMessage, 3000).show();
                            }
                        };

                        handlerException.post(rExceptionThread);

                        if(strException.indexOf("reset") != -1 || strException.indexOf("rejected") != -1)
                        {
                            isConnected = false;
                            try 
                            {
                                connectionSocket.close();
                            }
                            catch (IOException e1) 
                            {
                                e1.printStackTrace();
                            }

                        }

                        isConnected = false;
                    }

                }   
4

1 回答 1

1

好吧,您正在检查读取的字节数而不是每个单独的字节。

j = streamInput.read(arrayOfByte, 0, i);

返回读取的字节数并将这些字节放入 arrayOfByte。

因此,您需要执行以下操作:

for (int n=0; n < j; n++)
{
    if (arrayOfByte[n]  == hex) Log.d("Console", "Yo, daddio!");
}
于 2013-05-08T09:23:36.490 回答