0
私はサーバーからデータを受け取るために使用するソケットクライアントを持っています。エミュレータではすべて正常に動作しますが、私のアンドロイドではメッセージが常に遅れているため、GUIは新しいデータで更新されません。bufferedreaderは実際のデバイスで1行遅れです
コード関与(クライアントがAsynctaskで実行)
@Override
protected Boolean doInBackground(Void... params) { //This runs on a different thread
try {
Log.i("AsyncTask", "doInBackground: Creating socket");
SocketAddress sockaddr = new InetSocketAddress("192.168.x.xxx", 9090);
nsocket = new Socket();
nsocket.connect(sockaddr, 5000); //10 second connection timeout
if (nsocket.isConnected()) {
in = new BufferedReader(new InputStreamReader(nsocket.getInputStream()));
wr = new BufferedWriter(new OutputStreamWriter(nsocket.getOutputStream()));
Log.i("KMC.AsyncTask", "doInBackground: Socket created, streams assigned");
sockState = true;
String inputLine = "";
while ((inputLine = in.readLine()) != null) {
result = inputLine.replace("\\","");
Log.d("KMC", "Got some data: " + result);
this.publishProgress(1);
}
}
} catch (IOException e) {
e.printStackTrace();
Log.i("KMC.AsyncTask", "doInBackground: IOException");
} catch (Exception e) {
e.printStackTrace();
Log.i("KMC.AsyncTask", "doInBackground: Exception");
} finally {
try {
nis.close();
wr.close();
nsocket.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
Log.i("KMC.AsyncTask", "doInBackground: Finished");
}
return true;
}
protected void onProgressUpdate(Integer... values) {
try {
JSONObject jObject = new JSONObject(result.substring(1, result.length()-1));
if(jObject.getString("type").equals("event")){
EventHandler.newEvent(jObject.getString("name"), jObject.getJSONArray("data"));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
私はGoogleで解決策を見つけることを試みましたが、私はこの上で何かを見つけることができません。
これを参照してください。http://stackoverflow.com/questions/706105/is-there-an-equivalent-of-bufferedreader-readline-that-lets-me-pick-what-my-en –
"\ r \ n "#:。それ以外に、それはエミュレータではうまく動作しますが、私の電話では動作しません(ICSとジンジャー上) –