我在互联网上找到了一些套接字编程的例子,并尝试为以太网启用的Arduino服务器构建自己的Android客户端。但我遇到了两个问题。 首先,我的主要活动代码:
package com.domiflichi.TesterProject;
import java.io.BufferedWriter; // output
import java.io.BufferedReader; // input
import java.io.InputStreamReader; // input
import java.io.OutputStreamWriter; // output
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class TesterProjectMain extends Activity implements OnClickListener
{
private Button connectPhones;
private TextView myTextView; // represents the 'status text'
private String serverIpAddress = "";
private boolean connected = false;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.client);
connectPhones = (Button) findViewById(R.id.connect_phones);
connectPhones.setOnClickListener(connectListener);
connectPhones.setOnClickListener(this);
myTextView = (TextView) findViewById(R.id.text1);
}
// This was created when using the 'implements OnClickListener' in the class
public void onClick(View v) {
if (v.getId() == R.id.connect_phones) {
if (!connected) {
serverIpAddress = "192.168.0.178";
if (!serverIpAddress.equals("")) {
Thread cThread = new Thread(new ClientThread());
cThread.start();
connectPhones.setEnabled(false); // Once the button is pressed, disable it. :)
}
}
} else if (v.getId() == R.id.status_req) {
// CODE HERE FOR STATUS REQUEST BUTTON?
} else if (v.getId() == R.id.cmd_toggle) {
// CODE HERE FOR TOGGLE DOOR BUTTON?
} else if (v.getId() == R.id.cmd_crack) {
// CODE HERE FOR CRACK BUTTON?
} else if (v.getId() == R.id.disconnect) {
// CODE HERE FOR DISCONNECT BUTTON?
}
}
public class ClientThread implements Runnable {
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Log.d("ClientActivity", "C: Connecting...");
Socket socket = new Socket(serverAddr, 23);
connected = true;
while (connected) {
try {
Log.d("ClientActivity", "C: Sending command.");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
out.println("mypass*");
Log.d("ClientActivity", "C: Sent.");
BufferedReader r = new BufferedReader(new InputStreamReader(socket.getInputStream()));
final StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
Log.d("Server response", line.toString());
}
handler.post(new Runnable() {
public void run() {
if (total.toString().contentEquals("status:open")) {
myTextView.setText(R.string.status_open);
} else {
myTextView.setText(R.string.status_closed);
}
}
});
connected = false;
} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
}
socket.close();
Log.d("ClientActivity", "C: Closed.");
} catch (Exception e) {
Log.e("ClientActivity", "C: Error", e);
connected = false;
}
}
}
}
我的两个问题是:
- 我不知道如何使我的按钮与发送命令到服务器的套接字进行交互。连接后,我有(4)个按钮需要通过套接字发送各种命令(套接字在通过单击“连接”按钮启动的单独线程中运行)。 (寻找“// CODE HERE FOR STATUS REQUEST BUTTON?”评论,以了解我期望放置各种按钮代码的位置)
- 我需要将
'handler.post(new Runnable() {'
(while ((line = r.readLine()) != null) {
total.append(line);
Log.d("Server response", line.toString());
}
然而,当我这样做时,并且改变:
if (total.toString().contentEquals("status:open")) {
到
if (line.toString().contentEquals("status:open")) {
(因为我只想一次读取一行) Eclipse报错如下: 无法引用在不同方法中定义的内部类中的非最终变量行。
如果我尝试更改上面的行...: String line; 到 static String line;
Eclipse会在下一行报错:
while ((line = r.readLine()) != null) { 说: 最终局部变量行可能已经被分配。
我甚至不敢相信我已经走了这么远,因为我是一个完全的新手,但现在我遇到了难题。