Handler 收到的字符串长度经常会少一个,我发的99个字节,经常只收到98个呢,请高手指点一下。
package com.example.control_air;
/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Socket socket = null;// 开辟一个socket控件
Button enterbut = null;//
EditText IP = null;
EditText PORT = null;
EditText Aozero = null;// 温度
EditText Aofull = null;// 湿度
Button butLED1 = null;
Button butLED2 = null;
Button but3 = null;
Button but4 = null;
OutputStream outputstream;
InputStream in;
CheckBox TENcheck;
CheckBox ZEROcheck;
CheckBox FULLcheck;
TextView Btesttext=null;
EditText errortext = null;
EditText msglong = null;
TextView totaltext = null;
TextView TestHex=null;
boolean isConnected = false;
boolean led1data = false, led2data = false;
int a;
byte ledstatu=0;
Short b;
String xx;
byte butf[];
byte abutf[];
int errortimes=0,totaltimes=0;
static BufferedReader mBufferedReaderServer = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enterbut = (Button) findViewById(R.id.conn);// 获取ID号
IP = (EditText) findViewById(R.id.editIp);
IP.setText("10.10.100.254");
//IP.setText("192.168.16.103");
PORT = (EditText) findViewById(R.id.editport);
PORT.setText("8899");
edttemp = (TextView) findViewById(R.id.edittemp);
edthumi = (TextView) findViewById(R.id.edithumi);
maxHW= (EditText)findViewById(R.id.MaxHW);
enterbut.setOnClickListener(new enterclick());
butLED1 = (Button) findViewById(R.id.BUTLED1);
butLED2 = (Button) findViewById(R.id.BUTLED2);
/*
* 发送数据按键
*/
/*public class sendzeroclick implements OnClickListener {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (socket != null) {
try {
OutputStream outputstream = socket.getOutputStream();
if (Aozero != null && Aozero.length()>0){
byte buf[] = new byte[5];// 开辟2个空间
buf[0] =1;
buf[1] =2;
buf[2] =3;
buf[3] =4;
buf[4] =5;
outputstream.write(buf);
outputstream.flush();
Toast.makeText(MainActivity.this, "数据发送成功!", 0).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 连接输出流
} else
Toast.makeText(MainActivity.this, "WIFI连接错误,请检查!", 0).show();
}
}
Handler mHandler = new Handler() { // 等待socket连接成功
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String a, LED1status, LED2status;
String b ,c ,d;
int xx=9;
//Thread.sleep(500);
switch (msg.what) {
case 0:// TCP断开连接
enterbut.setText("建立连接");
Toast.makeText(MainActivity.this, "服务器连接断开!", 0).show();
break;
case 1:// 表明TCP连接成功,可以进行数据交互了
enterbut.setText("断开连接");
Toast.makeText(MainActivity.this, "服务器连接成功!", 0).show();
new InputThread().start();// 开启接收线程
// edttemp.setText("456");
break;
case 2:// 有数据进来
String result = msg.getData().get("msg").toString();
TestHex.setText(String.valueOf(result.length()));
if (++totaltimes>100){
totaltimes=0;
}
TestHex.setText("数据包长度: "+ String.valueOf(result.length()));
totaltext.setText("通讯包数: "+ String.valueOf(totaltimes));
break;
}
}
};
/**************** 定义按键触发事件函数 *******************/
public class enterclick implements OnClickListener {
@Override
public void onClick(View arg0) {
if (!isConnected)// 没有连接上
new ClientThread().start();// 打开连接
else// 连接上,断开连接
{
if (socket != null) {
try {
socket.close();
socket = null;
isConnected = false;
// 得到一个消息对象,Message类是有Android操作系统提供
Message msg = mHandler.obtainMessage();
msg.what = 0;
mHandler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
// 开辟一个线程 ,线程不允许更新UI socket连接使用
public class ClientThread extends Thread {
public void run() {
try {
socket = new Socket(IP.getText().toString(),Integer.parseInt(PORT.getText().toString()));// 建立好连接之后,就可以进行数据通讯了
isConnected = true;
in = socket.getInputStream();
// 得到一个消息对象,Message类是有Android操作系统提供
Message msg = mHandler.obtainMessage();
msg.what = 1;
mHandler.sendMessage(msg);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}// 连接服务器
}
}
public class InputThread extends Thread {
public void run() {
while (true) {
if (socket != null) {
String datain = readFromInputStream(in);
try {
if (!datain.equals("")) {
Message msg = new Message();
msg.what = 2;
Bundle data = new Bundle();
data.putString("msg", datain);
msg.setData(data);
mHandler.sendMessage(msg);
}
} catch (Exception e) {
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public String readFromInputStream(InputStream in) {
int count = 0;
byte[] inDatas = null;
try {
while (count == 0) {
count = in.available();
}
inDatas = new byte[count];
in.read(inDatas);
return new String(inDatas, "gb2312");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/******************************************
* @return
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Socket socket = null;// 开辟一个socket控件
Button enterbut = null;//
EditText IP = null;
EditText PORT = null;
EditText Aozero = null;// 温度
EditText Aofull = null;// 湿度
Button butLED1 = null;
Button butLED2 = null;
Button but3 = null;
Button but4 = null;
OutputStream outputstream;
InputStream in;
CheckBox TENcheck;
CheckBox ZEROcheck;
CheckBox FULLcheck;
TextView Btesttext=null;
EditText errortext = null;
EditText msglong = null;
TextView totaltext = null;
TextView TestHex=null;
boolean isConnected = false;
boolean led1data = false, led2data = false;
int a;
byte ledstatu=0;
Short b;
String xx;
byte butf[];
byte abutf[];
int errortimes=0,totaltimes=0;
static BufferedReader mBufferedReaderServer = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enterbut = (Button) findViewById(R.id.conn);// 获取ID号
IP = (EditText) findViewById(R.id.editIp);
IP.setText("10.10.100.254");
//IP.setText("192.168.16.103");
PORT = (EditText) findViewById(R.id.editport);
PORT.setText("8899");
edttemp = (TextView) findViewById(R.id.edittemp);
edthumi = (TextView) findViewById(R.id.edithumi);
maxHW= (EditText)findViewById(R.id.MaxHW);
enterbut.setOnClickListener(new enterclick());
butLED1 = (Button) findViewById(R.id.BUTLED1);
butLED2 = (Button) findViewById(R.id.BUTLED2);
/*
* 发送数据按键
*/
/*public class sendzeroclick implements OnClickListener {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (socket != null) {
try {
OutputStream outputstream = socket.getOutputStream();
if (Aozero != null && Aozero.length()>0){
byte buf[] = new byte[5];// 开辟2个空间
buf[0] =1;
buf[1] =2;
buf[2] =3;
buf[3] =4;
buf[4] =5;
outputstream.write(buf);
outputstream.flush();
Toast.makeText(MainActivity.this, "数据发送成功!", 0).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 连接输出流
} else
Toast.makeText(MainActivity.this, "WIFI连接错误,请检查!", 0).show();
}
}
Handler mHandler = new Handler() { // 等待socket连接成功
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String a, LED1status, LED2status;
String b ,c ,d;
int xx=9;
//Thread.sleep(500);
switch (msg.what) {
case 0:// TCP断开连接
enterbut.setText("建立连接");
Toast.makeText(MainActivity.this, "服务器连接断开!", 0).show();
break;
case 1:// 表明TCP连接成功,可以进行数据交互了
enterbut.setText("断开连接");
Toast.makeText(MainActivity.this, "服务器连接成功!", 0).show();
new InputThread().start();// 开启接收线程
// edttemp.setText("456");
break;
case 2:// 有数据进来
String result = msg.getData().get("msg").toString();
TestHex.setText(String.valueOf(result.length()));
if (++totaltimes>100){
totaltimes=0;
}
TestHex.setText("数据包长度: "+ String.valueOf(result.length()));
totaltext.setText("通讯包数: "+ String.valueOf(totaltimes));
break;
}
}
};
/**************** 定义按键触发事件函数 *******************/
public class enterclick implements OnClickListener {
@Override
public void onClick(View arg0) {
if (!isConnected)// 没有连接上
new ClientThread().start();// 打开连接
else// 连接上,断开连接
{
if (socket != null) {
try {
socket.close();
socket = null;
isConnected = false;
// 得到一个消息对象,Message类是有Android操作系统提供
Message msg = mHandler.obtainMessage();
msg.what = 0;
mHandler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
// 开辟一个线程 ,线程不允许更新UI socket连接使用
public class ClientThread extends Thread {
public void run() {
try {
socket = new Socket(IP.getText().toString(),Integer.parseInt(PORT.getText().toString()));// 建立好连接之后,就可以进行数据通讯了
isConnected = true;
in = socket.getInputStream();
// 得到一个消息对象,Message类是有Android操作系统提供
Message msg = mHandler.obtainMessage();
msg.what = 1;
mHandler.sendMessage(msg);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}// 连接服务器
}
}
public class InputThread extends Thread {
public void run() {
while (true) {
if (socket != null) {
String datain = readFromInputStream(in);
try {
if (!datain.equals("")) {
Message msg = new Message();
msg.what = 2;
Bundle data = new Bundle();
data.putString("msg", datain);
msg.setData(data);
mHandler.sendMessage(msg);
}
} catch (Exception e) {
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public String readFromInputStream(InputStream in) {
int count = 0;
byte[] inDatas = null;
try {
while (count == 0) {
count = in.available();
}
inDatas = new byte[count];
in.read(inDatas);
return new String(inDatas, "gb2312");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/******************************************
* @return
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
...全文
相关推荐
iOS中textField限制字符串长度、字符数的方法
主要给大家介绍了关于iOS中textField限制字符串长度、字符数的相关资料,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
【BlueZ 】util 模块:通用工具函数,源码中高频复用的基础组件
在 BlueZ 5.x 庞大的源码体系中,util 模块扮演着"幕后英雄"的角色。它不是直接处理蓝牙协议栈核心逻辑的主角,却是支撑所有模块运转的基础设施。从 HCI 命令封包的字节序处理、设备地址的格式转换,到 GATT 服务 UUID 的可读化显示、调试日志的十六进制输出,几乎每一个模块都在高频复用 util 模块提供的通用工具函数。
STM32 硬件错误HardFault_Handler的真凶1
(2)堆区(heap):一般由程序员分配和释放,若程序员不释放,程序结束时可能由操作系统回收 (3)全局区(静态区)(static):全局变量和静态变量的存储是
pb中实现字符长度和计算器的实现
初学者pb实现测字符长度 和计算器的实现 初学者很好的学习资源
golang-http-handler
golang-http-handler


