0
任何人都可以讓我知道在下面的代碼中有什麼錯誤。爲什麼它不執行while循環塊? 我在清單文件中擁有必要的權限。Android中的簡單插座示例問題
public class MainActivity extends Activity {
static TextView t;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t=(TextView)findViewById(R.id.txt);
NetConnect th=new NetConnect();
th.start();
}
public class NetConnect extends Thread {
public void run(){
try{
runOnUiThread(new Runnable(){public void run(){t.append("Thread start...");}});
Socket client = new Socket("time-b.nist.gov", 13);
BufferedReader in =new BufferedReader(new InputStreamReader(client.getInputStream()));
String str;
while((str=in.readLine())!=null)
t.append(str);
}catch(Exception e){
Log.e("Internet:",e.toString());
}
}
}
您是否在LogCat中看到任何錯誤?我想象一個異常正在被拋出。 –
順便說一句你應該關閉BufferedReader和套接字。 –
關閉部分是好的亞歷杭德羅..我需要關閉..沒有錯誤,沒有任何打印在LogCat –