1
我在Android 2.2手機上出現了一些奇怪的問題。無法關閉InputStreamReader
我打開套接字連接:
socket = new Socket(serverAddr, SERVERPORT);
messageWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), Charset.forName("UTF-8"))));
messageReader = new InputStreamReader(socket.getInputStream(), Charset.forName("UTF-8"));
開始在的AsyncTask doInBackground()閱讀:
protected Boolean doInBackground(final Void... unused) {
...
char[] chars = new char[256];
int len;
while((len = messageReader.read(chars))>=0) {
builder.append(chars, 0, len);
break;
}
result = true;
...
}
看完後我關閉連接:
try {
socket.close();
socket = null;
} catch (Exception e) {Log.e("socket", "ex");}
try {
messageWriter.close();
messageWriter = null;
} catch (Exception e) {Log.e("writer", "ex");}
try {
messageReader.close();
messageReader = null;
} catch (Exception e) {Log.e("reader", "ex");}
一切正常Android 2.3手機和Android 4.0模擬器,但由於某些原因,它不適用於Androi d 2.2手機。 套接字已關閉,寫入器已關閉,但讀卡器仍處於while循環中,即使在調用「socket.close();」...因此我的關閉代碼卡在「messageReader.close ();」整個用戶界面被阻止! ...並沒有辦法殺死AsyncTask ...爲什麼會發生這種情況,我該怎麼辦?
完美,謝謝!!!! – mgulan
@EJP能否爲您的申請提供參考? Javadoc聲稱與此相反:「在此套接字上的I/O操作中當前阻塞的任何線程將引發SocketException。」 [鏈接](http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#close%28%29) – user3259330