android http连接时间设置,如何在Android上设置Http连接超时
我在写一个连接到web服务的应用程序,如果它无法获得连接,我不希望它等待太久。因此,我设置了httpparams的connectionTimeout。但它似乎没有任何影响。如何在Android上设置Http连接超时这里是我的代码public void doPost(ArrayList list) {HttpPost post = new HttpPost(URL);HttpParams httpP
我在写一个连接到web服务的应用程序,如果它无法获得连接,我不希望它等待太久。因此,我设置了httpparams的connectionTimeout。但它似乎没有任何影响。如何在Android上设置Http连接超时
这里是我的代码
public void doPost(ArrayList list) {
HttpPost post = new HttpPost(URL);
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);
httpClient.setParams(httpParameters);
try {
post.setEntity(new UrlEncodedFormEntity(list));
HttpResponse response = httpClient.execute(post);
Log.i("response ", response.toString());
Log.i("response code", ""
+ response.getStatusLine().getStatusCode());
if (response != null) {
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
result = convertStreamToString(instream);
Log.i("Result ", result);
// Closing the input stream will trigger connection release
instream.close();
}
} else {
Log.i("SERVER ERROR ", "Server Not responding");
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我现在加入异常
但我的日志美食dosent5秒内显示任何错误。它会给我2 3薄荷错误。
这里是错误
03-26 12:36:59.165: W/System.err(491): java.net.SocketException: The operation timed out
03-26 12:36:59.165: W/System.err(491): at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(Native Method)
03-26 12:36:59.165: W/System.err(491): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:115)
03-26 12:36:59.165: W/System.err(491): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:244)
03-26 12:36:59.165: W/System.err(491): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:533)
03-26 12:36:59.165: W/System.err(491): at java.net.Socket.connect(Socket.java:1055)
03-26 12:36:59.165: W/System.err(491): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
03-26 12:36:59.165: W/System.err(491): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
03-26 12:36:59.165: W/System.err(491): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
03-26 12:36:59.165: W/System.err(491): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
03-26 12:36:59.165: W/System.err(491): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
03-26 12:36:59.165: W/System.err(491): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
03-26 12:36:59.165: W/System.err(491): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-26 12:36:59.165: W/System.err(491): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
03-26 12:36:59.165: W/System.err(491): at com.raa.json.JsonParser1.getJSONFromUrl(JsonParser1.java:39)
03-26 12:36:59.165: W/System.err(491): at com.raa.activity.ProductListActivity$MyTask.doInBackground(ProductListActivity.java:168)
03-26 12:36:59.165: W/System.err(491): at com.raa.activity.ProductListActivity$MyTask.doInBackground(ProductListActivity.java:1)
03-26 12:36:59.165: W/System.err(491): at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-26 12:36:59.165: W/System.err(491): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-26 12:36:59.165: W/System.err(491): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-26 12:36:59.165: W/System.err(491): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-26 12:36:59.165: W/System.err(491): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-26 12:36:59.165: W/System.err(491): at java.lang.Thread.run(Thread.java:1096)
更多推荐
所有评论(0)