如何在Java中为Android设置HttpResponse超时

如何在Java中为Android设置HttpResponse超时,第1张

如何在Java中为Android设置HttpResponse超时

在我的示例中,设置了两个超时。连接超时抛出

java.net.SocketTimeoutException: Socket is not connected
,套接字超时
java.net.SocketTimeoutException: The operation timed out

HttpGet httpGet = new HttpGet(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);HttpResponse response = httpClient.execute(httpGet);

如果要设置任何现有HTTPClient的参数(例如

DefaultHttpClient
AndroidHttpClient)
,则可以使用函数
setParams()

httpClient.setParams(httpParameters);


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4889571.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-12
下一篇2022-11-11

发表评论

登录后才能评论

评论列表(0条)

    保存