私はBufferedReaderを持っていますが、読み込もうとするとハングアップして何もしません。私はAsyncTaskでこれを使用しています。非同期タスク、BufferedReader
- 編集: Iを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)); }
- EDIT私はメインスレッドでそれを望んでいなかったので。
- を編集してください。完全なコードはここにあります。
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