I want to show a progress bar in my activity which contains code to test server connection using socket. I want my progress bar to be visible only when sending data to server. As soon as i got reply from server, the progress should be dismissed and shows Alert box with message “Server busy”. but in my screen the progress bar is visible after getting reply from server.Here is my code .
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"
}
}
so what should be done to have my progress bar to be visible before i get reply from server. If i get reply, the progress bar should be dismissed.
Any one please help me…
Here is how I have implemented progressbar while authenticating a user using AsyncTask. See if this can help you
And called this from my LoginActivity as below