部分與服務器的連接已完成,但不能始終從inputstream讀取數據,只能讀取一次。sslsocket只能從inputstream讀取一次,爲什麼?
public void startSSLClient() throws IOException {
inf = new Inflater();
mSocketBufferReader = new BufferedReader(new InputStreamReader(mSslSocket.getInputStream()));
new Thread() {
boolean online =false;
String s;
@Override
public void run() {
try {
while (true) {
String str = mSocketBufferReader.readLine();
if (!TextUtils.isEmpty(str)) {
Logger.e("PacketReader","process packet : "+str);
}
}
}
catch (Exception e) {
System.out.println("Error: " + e);
}
}
}.start();
}
如果我嘗試這樣做,它總是從輸入流中讀取。
public void startSSLClient() throws IOException {
inf = new Inflater();
new Thread() {
boolean online =false;
String s;
@Override
public void run() {
try {
while (true) {
BufferedReader mSocketBufferReader = new BufferedReader(new InputStreamReader(mSslSocket.getInputStream()));
String str = mSocketBufferReader.readLine();
if (!TextUtils.isEmpty(str)) {
Logger.e("PacketReader","process packet : "+str);
}
}
}
catch (Exception e) {
System.out.println("Error: " + e);
}
}
}.start();
}
我不知道原因。我只想使用全球領域。我不想總是使用新的本地字段。
不清楚你的問題是什麼,或者全球領域與它有什麼關係。你需要檢查結果'readLine()'爲null,並在你得到它時退出讀取循環。而且每次在循環中都不應該使用新的緩衝讀取器。 – EJP
@EJP'if(!TextUtils.isEmpty(str))'檢查結果爲null。我想要始終從inputstream讀取數據。我可以第一次閱讀。然後,我將keeplive消息發送給服務器,服務器響應客戶端。 –
它也檢查它是否爲空,並且如果您得到空值,則不退出循環。如果它僅僅是空的,你不想退出('「」')。你有什麼是不夠的。編寫該循環的常用方法是while((str = reader.readLine())!= null)'。 – EJP