0

Here is my method to connect to server and get the response in MyUtilitiesActivity (timeout code based on this answer by kuester2000).

 private static response = "";
public static String send(final Activity act, final String url, final String keys[], final String values[]) 
{ 
    try{
        HttpParams httpParameters = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.

        HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
        // Set the default socket timeout (SO_TIMEOUT) 
        // in milliseconds which is the timeout for waiting for data.

        HttpConnectionParams.setSoTimeout(httpParameters, 5000);  

        HttpClient httpclient = new DefaultHttpClient(httpParameters);
        if(keys == null && values == null)
        {

            HttpGet get = new HttpGet(url);   
            // Execute HTTP Get Request 
            response = httpclient.execute(get, new BasicResponseHandler()).trim();  
             
        }
        else if(keys != null && values != null && keys.length == values.length)
        {
            // Add your data   
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            for (int i = 0; i < values.length; i++)
            {
                nameValuePairs.add(new BasicNameValuePair(keys[i], values[i])); 
            }

            HttpPost post = new HttpPost(url);
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            response = httpclient.execute(post, new BasicResponseHandler()).trim();  

        } 

    }
    catch (Exception e) 
    {
        response = e + "";
        e.printStackTrace();
    }
    return response ;  

}

And i'am calling the above method on click of a button in MyTestActivity.

public onClick(View v)
{
String response =  MyUtilitiesActivity.send(MyTestActivity.this,    "http://www.google.com", null, null);

 //the code to be execute after getting the response from server
 }

Where do i display the progress bar? Inside onclick of a button or inside send() method? How to display it ? Please help me.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Rahul Baradia
  • 11,802
  • 17
  • 73
  • 121

2 Answers2

7

Use AsyncTask concept which is known as Painless Threading in Android.

  1. Put your send() method in Async's doInBackGround()
  2. Start progress dialog at onPreExecute
  3. and dismisss on onPostExecute().
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
user370305
  • 108,599
  • 23
  • 164
  • 151
  • Put your send() method in Async's doInBackGround() : I'an unable to do this. Shall i call send method in doInBG() or shall i put the code in doInBG(). Could you please explain in detail . Thank you. – Rahul Baradia Mar 22 '12 at 06:40
  • Yes you can put your send method's code in doInBack() also No Problem..! – user370305 Mar 22 '12 at 06:41
0

You can add the progress bar like this:

ProgressDialog dialog = ProgressDialog.show(YourActivity.this, "", 
                                    getString(R.string.Loading), true);

and when your long operation is gone, you should call dialog.dismiss();