BufferedReader를 읽으려고하면 그냥 멈추고 아무 것도하지 않습니다.이 작업을 수행하고 있습니까? 나는 이것을 AsyncTask에서 사용하고있다.비동기 작업, BufferedReader
- 편집 : 내가는 Wi-Fi에 연결 태블릿, 이것은 스레드가 시작할 때, 내가 볼 수있는 포트 (5334)에 172.20.104.203에 방송, 아무것도하지만 그 후 내 컴퓨터에 연결합니다. 여기
내 코드 : 나는 모든 권리 권한 또는 아무것도, 나는 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();
}
}
}
}
}
}
다른쪽에있는 항목은 무엇입니까? readline의 문제는 소켓이 연결이 끊어 지거나 새로운 라인이 보내질 때까지 리턴하지 않을 것이라는 것입니다. – MByD
전체 코드를 원합니까? – FabianCook
전체 코드가 아니라 통신의 본질. – MByD