我有以下主类和线程TCP客户端。客户端在循环中运行,接收消息并将其传递给主类。在主类中,我解析消息并尝试根据收到的消息的名称和值显示不同的图像。 例如:shiftDirection1 名称:shiftDirection&价值:1
但是我只能显示与第一个接收到的消息对应的图像,并且无法显示与剩余接收的消息对应的图像。
请仔细阅读以下代码,并提出错误/问题以及替代方法。
感谢您的时间和精力。
马杜
主要课程:
public class TCPListen extends Activity implements TCPListener {
private TextView mTitle;
public String recData[] = new String[2];
String PresentGear = "0";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TcpServiceHandler handler = new TcpServiceHandler(this,this);
Thread th = new Thread(handler);
th.start();
}
public String[] callCompleted(String source){
//Log.d("TCP", "Std parser " + source);
//mTitle.setText(source);
//String data[] = new String[2];
//if (source.matches("<MSG><N>.*</N><V>.*</V></MSG>")) {
Document doc = null;
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = (Document) db.parse(new ByteArrayInputStream(source.getBytes()));
NodeList n = doc.getElementsByTagName("N");
Node nd = n.item(0);
String msgName = nd.getFirstChild().getNodeValue();
NodeList n1 = doc.getElementsByTagName("V");
Node nd1 = n1.item(0);
String tmpVal = nd1.getFirstChild().getNodeValue();
recData[0] = msgName;
recData[1] = tmpVal;
if (recData[0].equals("currGear")) PresentGear = recData[1];
Log.d("TCP", "Inside Std parser " + recData[0] + " " + recData[1]);
actionOnData(recData[0], recData[1]);
}
catch(Exception e){
e.printStackTrace();
}
Log.d("TCP", "Just outside Std parser " + recData[0] + " " + recData[1]);
return recData;
//} else Log.d("TCP", "Message in wrong format " + source);
//mTitle.setText("Message in wrong format " + source);
//return data;
}
//Function to display driver messages/images based on individual messages
public void actionOnData(String name, String value) {
String tempName = name;
String tempVal = value;
setContentView(R.layout.image);
ImageView showImage = (ImageView) findViewById(R.id.imageView1);
//Log.d("TCP", "------------>" + tempName + " " + tempVal);
if (tempName.equals("shiftDirection") && tempVal.equals("1")) {
//setContentView(R.layout.image);
//TextView text_top = (TextView) findViewById(R.id.textView1);
//showImage = (ImageView) findViewById(R.id.imageView1);
//text_bottom.setText(Info[1]);
showImage.setImageResource(R.drawable.shift_up);
Log.d("TCP", "1------------>" + showImage);
} else if (tempName.equals("shiftDirection") && tempVal.equals("-1")) {
//setContentView(R.layout.image);
//TextView text_bottom = (TextView) findViewById(R.id.textView2);
//Resources res = getResources();
//Drawable drawable = res.getDrawable(R.drawable.shift_down);
//showImage = (ImageView) findViewById(R.id.imageView1);
//text_bottom.setText(Info[1]);
showImage.setImageResource(R.drawable.shift_down);
} else if (tempName.equals("recomGear") && tempVal != null) {
Log.d("TCP", "3------------>" + tempName + " " + tempVal);
Integer msgValue = Integer.parseInt(recData[1]);
//Integer CurrentGear = (msgValue) - 1;
//Log.d("TCP","in DA Images. Current gear: " + CurrentGear);
//String Gear = Integer.toString(CurrentGear);
setContentView(R.layout.image);
TextView text_top = (TextView) findViewById(R.id.textView1);
TextView text_bottom = (TextView) findViewById(R.id.textView2);
showImage = (ImageView) findViewById(R.id.imageView1);
showImage.setImageResource(R.drawable.shift_up);
text_bottom.setText(PresentGear);
text_top.setText(tempVal);
} else if (tempName.equals("currGear") && tempVal != null) {
Log.d("TCP", "4------------>" + tempName + " " + tempVal);
PresentGear = tempVal;
//Log.d("TCP","in DA Images. Present gear1: " + PresentGear);
setContentView(R.layout.image);
TextView text_bottom = (TextView) findViewById(R.id.textView2);
text_bottom.setText(PresentGear);
} else if (tempName.equals("shiftDirection") && tempVal.equals("0")) {
Log.d("TCP", "5------------>" + tempName + " " + tempVal);
Log.d("TCP","in DA Images. Present gear: " + PresentGear);
setContentView(R.layout.image);
TextView text_bottom = (TextView) findViewById(R.id.textView2);
//TextView text_top = (TextView) findViewById(R.id.textView1);
//text_top.setText("Go on");
text_bottom.setText(PresentGear);
}
}
}
仅显示与第一个if情况对应的图像。程序控件进入第二个if循环,但不显示那里的图像。
接口:
public interface TCPListener {
public String[] callCompleted(String msg);
}
线程(TCP客户端):
public class TcpServiceHandler implements Runnable {
TCPListener _listener;
private Activity _act;
public BufferedReader in;
public TcpServiceHandler(TCPListener listener, Activity act){
_listener = listener;
_act = act;
}
public synchronized void run() {
// TODO Auto-generated method stub
//if(socket==null){
try {
//InetAddress serverAddr = InetAddress.getByName("192.168.178.25");
Socket socket = new Socket("192.168.62.23", 1200, true);
//
//while(true){
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
final int delay = 100;
final Timer _timer = new Timer();
_timer.scheduleAtFixedRate(new TimerTask() {
public void run(){
String str;
try {
str = in.readLine();
_listener.callCompleted(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 0, delay);
//final String str = in.readLine();
//this._act.runOnUiThread(new Runnable(){
//public void run() {
// _listener.callCompleted(str);
// }
//});
}
catch(Exception e){
e.printStackTrace();
}
//}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
你可以检查两件事:
您的TcpServiceHandler
是线程的Runnable
,但run()
中没有循环。你在这个方法中定义的_timer可能已经死了,并且在它完成工作之前已经消失了。
您是否正在从后台线程更改UI?这一般不是一个好主意。
检查AsyncTask
这是一个在后台运行操作的便捷工具。