私は変数の現在の状態を更新するこのプログラムを作成しようとしています。私がどのように動作させるかは、文字列 "update"をサーバーに絶えず送信するための時間を掛けたタスクを持つことです。サーバーは文字列を認識し、アンドロイドデバイスの変数に必要な値を送信します。しかし、私はいくつかの問題に直面しています。文字列「更新」はエラーなしで送信されますが、サーバーからの対応する値が返されると、プログラムは応答を読み取れないように見えます。ソケットインプットストリームから読み込み、アンドロイドUIがフリーズする
//Open socket and initialize data streams
try {
socket = new Socket(serverIpAddress, applicationport);
//in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//in = new DataInputStream(socket.getInputStream());
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())), true);
} catch (UnknownHostException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
ShowDialog("Login Error" + ex.getMessage());
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
ShowDialog("Login Error" + ex.getMessage());
}
//Create new daemon timer
updateData = new Timer(true);
updateData.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
out.println("update");
UpdateMethod();
}//run
}, 1000, 10000);//schedule the delays start/interval here
};
private void UpdateMethod() {
//This method is called directly by the timer
//and runs in the same thread as the timer.
//It calls the method that will work with the UI
//through the runOnUiThread method.
this.runOnUiThread(Timer_Tick);
};//timermethod
private Runnable Timer_Tick = new Runnable() {
public void run() {
try {
//in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//String getx1 = null;
//getx1 = in.readLine();
//if (getx1 != null) {
//float updatex1 = Float.parseFloat(getx1);
//get_x1 = getx1;
//}
in = new DataInputStream(socket.getInputStream());
/*try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
x1display = (TextView) findViewById(R.id.x1display);
x1display.setText(in.readUTF());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
if (in != null){
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
};
あなたが見ることができるように、私ものDataInputStreamとBufferedReaderの両方を試してみましたが、無駄にしている:ここでは、コードです。
編集:入力ストリームから読み込もうとすると、UIがフリーズしているようです。私のコードがエラーなしであるように私は何が間違っているのか分かりません。
何か助けや助言をいただければ幸いです!
ありがとうございました!
使用して試してみました: InputStreamはsocket.getInputStream()です。 –
こんにちはHakem、私はこれをどのように実装することができますか詳細に説明できますか? ありがとう – Pooty
私は= new DataInputStream(socket.getInputStream());を置き換えることを意味しました。 InputStreamは= socket.getInputStream();です。私はそれがinputstreamを使用して働いている原因 –