在 java 中接收数据后无法将字符串转换为 int
cannot convert string to int after receiving data in java
我有发送 2 int 的 arduino,int 由“:”分开
例如我的 arduino 发送数据 = 12315: 15123
在我使用蓝牙发送该数据后,我在智能手机中接收到数据,我需要我接收到的 2 值再次变为 int
这是我 android
上的部分代码
void beginListenForData()
{
final Handler handler = new Handler();
final byte delimiter = 10; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler.post(new Runnable()
{
public void run()
{
String[] tmp;
String lol,dam;
tmp = data.split(":");
try
{
lol = String.valueOf(crypt[0])+":"+String.valueOf(crypt[1]);
derajat.setText(lol);
}
catch (NumberFormatException nfe)
{
// bad data - set to sentinel
crypt[0] = Integer.MIN_VALUE;
crypt[1] = Integer.MIN_VALUE;
derajat.setText("none");
}
dam = tmp[0]+":"+tmp[1];
myLabel.setText(dam);
//derajat.setText(data);
}
});
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
}
}
catch (IOException ex)
{
stopWorker = true;
}
}
}
});
workerThread.start();
}
如你所见,'\n' 之后的数据将被处理
首先,我使用 data.split() 拆分,我得到存储在 tmp 中的 2 个值
之后我将该值解析为 crypt[0] 和 crypt[1]
我将文本设置为 derajat 以查看我的值
我也在 myLabel 中设置文本结果
我可以在 myLabel 中看到我的值,但我无法将其转换为 int,因为我在 label derajat 中看不到任何内容
有人可以帮助我为什么我在收到该数据后无法转换 int 吗??
如果从您的 Arduino 接收到的数据确实如您指定的那样:
"12315: 15123"
然后发生的事情是你得到一个 NumberFormatException 你做了 catch但除了 derajat 将不包含任何内容(或 "none")之外,真的没有办法知道......这正是您正在经历的。
你会得到 NumberFormatException 的原因是因为在冒号 (:) 定界符之后有一个 white-space 所以当你尝试执行时:
crypt[1] = Integer.parseInt(tmp[1]);
(tmp[1] 实际上持有“15123”) 抛出 NumberFormatException。 Integer.parseInt() 方法不会处理数字字符串中的 white-space。要解决此问题 trim 您的拆分字符串,例如:
crypt[0] = Integer.parseInt(tmp[0].trim());
crypt[1] = Integer.parseInt(tmp[1].trim());
或拆分为:
tmp = data.split(": ");
我有发送 2 int 的 arduino,int 由“:”分开 例如我的 arduino 发送数据 = 12315: 15123
在我使用蓝牙发送该数据后,我在智能手机中接收到数据,我需要我接收到的 2 值再次变为 int 这是我 android
上的部分代码 void beginListenForData()
{
final Handler handler = new Handler();
final byte delimiter = 10; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler.post(new Runnable()
{
public void run()
{
String[] tmp;
String lol,dam;
tmp = data.split(":");
try
{
lol = String.valueOf(crypt[0])+":"+String.valueOf(crypt[1]);
derajat.setText(lol);
}
catch (NumberFormatException nfe)
{
// bad data - set to sentinel
crypt[0] = Integer.MIN_VALUE;
crypt[1] = Integer.MIN_VALUE;
derajat.setText("none");
}
dam = tmp[0]+":"+tmp[1];
myLabel.setText(dam);
//derajat.setText(data);
}
});
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
}
}
catch (IOException ex)
{
stopWorker = true;
}
}
}
});
workerThread.start();
}
如你所见,'\n' 之后的数据将被处理 首先,我使用 data.split() 拆分,我得到存储在 tmp 中的 2 个值 之后我将该值解析为 crypt[0] 和 crypt[1] 我将文本设置为 derajat 以查看我的值
我也在 myLabel 中设置文本结果 我可以在 myLabel 中看到我的值,但我无法将其转换为 int,因为我在 label derajat 中看不到任何内容 有人可以帮助我为什么我在收到该数据后无法转换 int 吗??
如果从您的 Arduino 接收到的数据确实如您指定的那样:
"12315: 15123"
然后发生的事情是你得到一个 NumberFormatException 你做了 catch但除了 derajat 将不包含任何内容(或 "none")之外,真的没有办法知道......这正是您正在经历的。
你会得到 NumberFormatException 的原因是因为在冒号 (:) 定界符之后有一个 white-space 所以当你尝试执行时:
crypt[1] = Integer.parseInt(tmp[1]);
(tmp[1] 实际上持有“15123”) 抛出 NumberFormatException。 Integer.parseInt() 方法不会处理数字字符串中的 white-space。要解决此问题 trim 您的拆分字符串,例如:
crypt[0] = Integer.parseInt(tmp[0].trim());
crypt[1] = Integer.parseInt(tmp[1].trim());
或拆分为:
tmp = data.split(": ");