我有一个BufferedReader,当我尝试阅读它时,它只是挂起而且没有做任何事情,我这样做了吗?我在AsyncTask中使用它。
这是我的代码:
try { final BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream())); String line = null; while ((line = in.readLine()) != null) { final String msg; msg = (line); Log.d("DeviceActivity", msg); } } catch (Exception e) { e.printStackTrace(); Log.e("ClientAcivtity: Exception", String.valueOf(e)); }
- 修改,这是完整的代码。
public class NetworkTask extends AsyncTask<Void, byte[], Boolean> {
Socket nsocket; // Network Socket
InputStream nis; // Network Input Stream
OutputStream nos; // Network Output Stream
private Handler handler = new Handler();
Boolean connected = false;
public static final int PORT = 5334;
public String SERVERIP = "172.20.104.203";
Socket socket;
@Override
protected void onPreExecute() {
Log.i("AsyncTask", "onPreExecute");
InetAddress serverAddr;
try {
serverAddr = InetAddress.getByName(SERVERIP);
socket = new Socket(serverAddr, PORT);
connected = true;
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("ClientAcivtity: Exception", String.valueOf(e));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("ClientAcivtity: Exception", String.valueOf(e));
}
}
@Override
protected Boolean doInBackground(Void... params) { // This runs on a
// different thread
boolean result = false;
try {
Log.d("ClientActivity", "C: Connecting...");
if (socket != null) {
int cont = 1;
while (cont == 1) {
try {
Log.d("ClientActivity", "C: Sending command.");
PrintWriter out = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())), true);
// where you issue the commands
out.println("getPos");
Log.d("ClientActivity", "C: Sent " + "getPos");
} catch (Exception e) {
Log.e("ClientAcivtity: Exception",
String.valueOf(e));
}
try {
final BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
final String msg;
msg = (line);
Log.d("DeviceActivity", msg);
}
} catch (Exception e) {
e.printStackTrace();
Log.e("ClientAcivtity: Exception",
String.valueOf(e));
}
cont--;
}
Log.d("ClientActivity", "C: Closed.");
}
} catch (Exception e) {
Log.e("ClientAcivtity: Exception", String.valueOf(e));
}
return result;
}
@Override
protected void onProgressUpdate(byte[]... values) {
if (values.length > 0) {
Log.i("AsyncTask", "onProgressUpdate: " + values[0].length
+ " bytes received.");
}
}
@Override
protected void onCancelled() {
Log.i("AsyncTask", "Cancelled.");
}
@Override
protected void onPostExecute(Boolean result) {
if (socket != null) {
if (connected) {
if (result) {
Log.i("AsyncTask",
"onPostExecute: Completed with an Error.");
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Log.i("AsyncTask", "onPostExecute: Completed.");
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
答案 0 :(得分:0)
尝试这样做:
final BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
StringBuffer buf = new StringBuffer();
int i;
while((i = in.read()) != -1){
buf.append((char) i);
}
String data = buf.toString();
答案 1 :(得分:0)
从套接字读取是一个非常困难的问题,取决于套接字实际连接的位置以及另一方如何响应。
如果另一方速度非常快,它可以为套接字提供足够的数据,以便读取例程实际上正常工作。但是如果在任何类型的另一侧有一个延迟(只需要比你的读取例程慢,包括小的默认超时)那么你的读取就会失败,即使另一方可能有数据 - 只是到达有点太慢在插座上。
根据您的需要,您可以围绕读取程序包装自己的最小和最大计时器。
请提供更多信息,我们可以更好地了解这个问题。
在许多情况下,必须使最小超时足够大,以便另一方将数据推送到套接字 - 但是您可能还需要最长时间来确定等待数据到达的时间。
更新:
首先运行runnable来启动监控线程。如果需要,您可以在循环中使用monitoringCanRun
来中断线程。并且monitoringThreadIsAlive
可用于了解线程是否仍在运行。
monitoringCanRun = true;
new Thread(new Runnable() {
public void run() {
monitoringThreadIsAlive = true;
performMonitoring();
monitoringThreadIsAlive = false;
}
}).start();
}
和performMonitoring看起来像:
public void performMonitoring() {
while (monitoringCanRun) {
... do your read in the while loop
...you might like to insert some delay before trying again...
try { //we delay every partial read so we are not too fast for the other side
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 2 :(得分:0)
我的猜测是当你写出命令“getPos”时,底层的BufferedWriter实际上并没有在线上发送数据(你应该用tcpdump / wireshark验证这一点)。如果是这种情况,服务器不响应readLine(),因为它从未得到命令。要验证此声明,请在out.flush();
out.println("getPos");
真的,tcpdump可能会给你一个比论坛上任何人更好的答案。
另见http://developer.android.com/reference/java/io/BufferedWriter.html