我想在我的活动中显示一个进度条,其中包含使用套接字测试服务器连接的代码。我希望我的进度条仅在向服务器发送数据时可见。一旦我收到服务器的回复,进度应该被取消并显示带有消息“服务器忙”的警报框。但在我的屏幕上,从服务器获得回复后进度条可见。这是我的代码。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mProgress = (ProgressBar) findViewById(R.id.progressBar1);
mProgress.setProgress(0);
checkdb();
}
private void checkdb() {
String message = "";
try {
serverIpAddress = InetAddress.getByName("192.168.1.133");
Log.d("TCP", "C: Connecting...");
socketObject = new Socket(serverIpAddress, 8221);
String str = "hi";
try {
Log.d("TCP", "C: Sending: '" + str + "'");
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socketObject.getOutputStream())), true);
out.println(str);
inputStream = socketObject.getInputStream();
inputDataStream = new DataInputStream(inputStream);
message = inputDataStream.readUTF();
Log.d("TCP", "C: Reply: '" + message + "'");
}
catch(IOException e)
{
Log.e("TCP", "S: Error", e);
}catch (Exception e) {
Log.e("TCP", "S: Error", e);
}
finally {
socketObject.close();
Log.e("TCP", "S: Error");
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
Log.e("TCP", "C: UnknownHostException", e);
e.printStackTrace();
}
catch(IOException e)
{
Log.e("TCP", "S: Error:", e);
//Code to show Alert box with message "IOException"
}
}
所以在我从服务器得到回复之前应该怎么做才能让我的进度条可见。如果我得到回复,进度条应该被关闭。任何人请帮助我...