0

Actually this is what i must do to set the Connection Timeout (from this answer by kuester2000):

HttpGet httpPost = new HttpGet("www.xxxx.com/method");
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used. 
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT) 
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(httpGet);

but in my application i make the httpClient as a Static field in the MainActivities and use it in all the other Activities. Just to have session.

so, should i have this code in all the Activities:

HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used. 
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT) 
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

and then

MainActivity.httpClient.setParams(httpParameters);

I just want to know, how i can set the params in the MainActivity and just use the httpClient in all the other Activitites rather than setting the params in every Activity.

Thank You

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226

2 Answers2

3

use a static code block in your main activity class to set the parameter.

public class MainActivity extends Activity {

    static final DefaultHttpClient httpClient;

    static {    
        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
        HttpConnectionParams.setSoTimeout(httpParameters, 5000);
        httpClient = new DefaultHttpClient(httpParameters);
    }


   ...

}
user1524957
  • 176
  • 2
  • 4
2

try this:

create variables like:

    private static final long CONN_MGR_TIMEOUT = 10000;
    private static final int CONN_TIMEOUT = 50000;
    private static final int SO_TIMEOUT = 50000;

and use this code with httppost:

    ConnManagerParams.setTimeout(params, CONN_MGR_TIMEOUT);
    HttpConnectionParams.setConnectionTimeout(params, CONN_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SO_TIMEOUT);
SubbaReddy PolamReddy
  • 2,083
  • 2
  • 17
  • 23
  • i can't use methods like setTimeout() during declaring the Fields. SO i have to use these methods in the Activity. Which mean in every Activity. – Archie.bpgc Sep 28 '12 at 06:29
  • i think ur requirement is httppost to the server without getting timeout excepetion .is it ?? – SubbaReddy PolamReddy Sep 28 '12 at 06:32
  • No. I have to set ConnectionTimeout params to the httpClient, only once. And use the same httpClient(prams already set in MainActivity) in all other Activities. Till now i was not setting any params, so i made a static field of this httpClient in the MainActivity and used it as MainActivity.httpClient in all other Activities – Archie.bpgc Sep 28 '12 at 06:36
  • what's ur requirement finally in brief?? – SubbaReddy PolamReddy Sep 28 '12 at 06:40
  • create a httpClient in the MainActivity as a field like. public static DefaultHttpClient httpclient = new DefaultHttpClient(); and setParameters in the MainActivity itself. And use it as MainActivity.httpclient in all other Activities – Archie.bpgc Sep 28 '12 at 06:43
  • That is the requirement. The problem is i must setParams to this httpclient in onCreate() of the MainActivity. So, this won't reflect on any other Activity using MainActivity.httpclient – Archie.bpgc Sep 28 '12 at 06:44
  • If i am using MainActivity.httpclient in other Activities i only get the httpclient without params set. So i have to set params in every Activity, after getting the client using MainActivity.httpclient. I want to know if there is a way to avoid setting params in every activity which uses this httpclient. I want to set the params only once. – Archie.bpgc Sep 28 '12 at 06:47
  • k... first u create httpclient class in that u create params what u required.. and those will be useful for main activity and also for other activities like setters and getters – SubbaReddy PolamReddy Sep 28 '12 at 06:50
  • Fine, i will try that. But i have 1 more question. If i set the ConnectionTime to some 5 secs. and in 5 secs the connection is not established what will be the out come? i mean is there any message. How can i track if i got a ConnectionTimeOut?>? Thank You – Archie.bpgc Sep 28 '12 at 07:18