实际上这是我必须做的设置连接超时:
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);
但是在我的应用程序中,我在 MainActivities中将其httpClient
作为 a并在所有其他活动中使用它。Static field
只是为了开会。
所以,我应该在所有活动中都有这个代码:
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);
接着
MainActivity.httpClient.setParams(httpParameters);
我只想知道,我如何在所有其他 Activityparams
中MainActivity
设置并只使用 httpClient,而不是在每个 Activity 中设置参数。
谢谢你