其实这是我必须做的设置连接超时:安卓:设置连接超时的HttpPost
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);
,但在我的应用程序使httpClient
作为MainActivities一个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);
我只是想知道,我怎么能在MainActivity
设置params
,只是使用所有其他Activitite中的httpClient,而不是在每个Activity中设置参数。
谢谢
我不能在声明字段时使用像setTimeout()这样的方法。所以我必须在活动中使用这些方法。这意味着每个活动。 –
我认为你的要求是服务器的httppost没有得到超时excepetion。 –
不需要。我必须将ConnectionTimeout参数设置为httpClient,只有一次。并在所有其他活动中使用相同的httpClient(已在MainActivity中设置了prams)。直到现在我还没有设置任何参数,所以我在MainActivity中创建了这个httpClient的一个静态字段,并在所有其他活动中使用它作为MainActivity.httpClient –