0
서버에서 데이터를 수신하는 데 사용하는 소켓 클라이언트가 있습니다. 에뮬레이터에서는 모든 것이 잘 작동하지만 내 안드로이드에서 메시지는 항상 뒤쪽에서 뒤쳐 지므로 내 GUI는 새 데이터로 업데이트되지 않습니다.bufferedreader는 실제 장치에서 한 줄 뒤에 있습니다.
관련된 코드 (클라이언트가 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();
}
}
내가 구글에 대한 해결책을 찾기 위해 노력했습니다,하지만 난이 아무것도 찾을 수 없습니다.
다음을 참조하십시오. http://stackoverflow.com/questions/706105/is-there-an-equivalent-of-bufferedreader-readline-that-lets-me-pick-what-my-en –
"\ r \ n "모든 전송합니다. 그 외에도 에뮬레이터에서는 잘 작동하지만 휴대 전화에서는 사용할 수 없습니다 (ICS 및 생강) –