android請求超時時間
『壹』 android中如何獲取超時時長的異常
android獲取超時時長的異常方式如下:設置超時機制
client.getParams().setIntParameter(
HttpConnectionParams.SO_TIMEOUT, TIME_OUT_DELAY); // 超時設置
client.getParams().setIntParameter(
HttpConnectionParams.CONNECTION_TIMEOUT, TIME_OUT_DELAY);// 連接超時
這里設置了兩種超時,第一種是請求超時,第二種時連接超時。
當向伺服器發出請求後,請求和伺服器建立socket連接,但是很長時間內都沒有建立socket連接,這就時第一種請求超時,這種情況主要發生在請求了
一個不存在的伺服器。超時之後,會拋出InterruptedIOException異常。
Timeout for blocking operations. The argument value is specified in
milliseconds. An InterruptedIOException is thrown if this timeout
expires.
『貳』 android 子線程請求伺服器10秒後若還不成功,通知用戶網路有問題
用timer計時 可以設定 多長時間後執行某個操作 也就是說 你可以寫一個通知方法 當子線程開始執行的時候 執行這個timer就好了
『叄』 Android Socket通信如何設置超時時間
其實關於這個問題可能用到的人不會很多,不過我在這里還是說說。
正常很多人寫socket通信時,都會直接通過new socket(IP,PORT)直接去鏈接伺服器。其實這種做法也沒有錯誤,但是若當伺服器IP不存在會伺服器沒有響應時,程序會卡在這句代碼老長一段時間,才會跳出並報異常。這對於這種問題,通過設置連接超時時間可以進行解決:
socket = new Socket();
SocketAddresssocAddress = new InetSocketAddress(this.netAdress, this.port);5000就是你所設置的超時時間!
『肆』 android java怎麼設置超時時間
第一,ConnectionPoolTimeout:
定義了從ConnectionManager管理的連接池中取出連接的超時時間。
出錯會拋出
第二,ConnectionTimeout:
定義了通過網路與伺服器建立連接的超時時間,Httpclient包中通過一個非同步線程去創建與伺服器的socket連接,這就是該socket連接的超時時間。
當連接HTTP伺服器或者等待HttpConnectionManager管理的一個有效連接超時出錯會拋出ConnectionTimeoutException
第三,SocketTimeout:
這定義了Socket讀數據的超時時間,即從伺服器獲取響應數據需要等待的時間。
當讀取或者接收Socket超時會拋出SocketTimeoutException
『伍』 android中http get請求總是超時怎麼辦
檢查網路,查看是否有許可權,httpclient框架實現示例代碼:
1. GET 方式傳遞參數
//先將參數放入List,再對參數進行URL編碼
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "數據")); //增加參數1
params.add(new BasicNameValuePair("param2", "value2"));//增加參數2
String param = URLEncodedUtils.format(params, "UTF-8");//對參數編碼
String baseUrl = "伺服器介面完整URL";
HttpGet getMethod = new HttpGet(baseUrl + "?" + param);//將URL與參數拼接
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse response = httpClient.execute(getMethod); //發起GET請求
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //獲取響應碼
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//獲取伺服器響應內容
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
2. POST方式 方式傳遞參數
//和GET方式一樣,先將參數放入List
params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "Post方法"));//增加參數1
params.add(new BasicNameValuePair("param2", "第二個參數"));//增加參數2
try {
HttpPost postMethod = new HttpPost(baseUrl);//創建一個post請求
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //將參數填入POST Entity中
HttpResponse response = httpClient.execute(postMethod); //執行POST方法
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //獲取響應碼
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //獲取響應內容
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
『陸』 android版本的app在wifi下請求伺服器會超時,但3G、4G下不會
1、首先我們就須要看看在蘋果手機里的設置蜂窩移動網路有沒有打開來,無法連接到itunesstore時,可能是手機中移動數據沒有打開起來而導致無法正常連接到網路。
2、打開蘋果手機裡面的設置,進入到蜂窩移動網路
3、把蜂窩移動數據打開起來,然後看看登陸你的Apple ID看看
方法、步驟二:
1、也有可能是手機裡面網路設置問題導致的,打開蘋果手機設置進入通用裡面。
2、按一下還原、還原網路設置
方法、步驟三:
Apple ID的問題,建議更換一下你登陸的Apple ID,通常在注冊Apple
ID時,一些信息沒有填寫完整也會導致蘋果手機無法連接到itunesstore,重新注冊一個Apple ID或者使用別人的Apple ID登陸看看。
『柒』 android OKHttp網路請求默認多長時間超時
OkHttp 處理了很多網路疑難雜症:會從很多常用的連接問題中自動恢復。如果您的伺服器配置了多個IP地址,當第一個IP連接失敗的時候,OkHttp會自動嘗試下一個IP。OkHttp還處理了代理伺服器問題和SSL握手失敗問題。
『捌』 如何在android下採用相對時間,實現超時等待的功能
一、函數功能說明
pthread_cond_timedwait 等待一個條件變數,或者超時就會返回
POSIX有兩種時鍾類型
1、CLOCK_REALTIME: 系統范圍內的實時時鍾,是個時鍾,可以通過命令等方式修改該系統時間.
2、CLOCK_MONOTONIC:系統起機時到現在的時間,不能被設置和修改.
pthread_cond_timedwait()在沒有設置條件變數屬性的時候,默認用的是CLOCK_REALTIME時間,
因此在極端情況下會出現實際等待的時間與設置的超時時間不同。
所以,對於linux的超時等待功能,最好是使用CLOCK_MONOTONIC進行實現,並且通過pthread_condattr_setclock實現。
而對於android系統而言,是不支持pthread_condattr_setclock,通過驗證可以採用函數pthread_cond_timedwait_monotonic實現。
下面直接給出代碼的實現功能。
二、超時等待功能
[cpp] view plain
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/times.h>
#include <unistd.h>
#include <time.h>
static pthread_mutex_t s_mut = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t s_cond = PTHREAD_COND_INITIALIZER;
void PthreadAttr_Init(void);
unsigned long long getSysTime(void);
void waitTimeout(void);
void PthreadAttr_Init(void)
{
#if defined(ANDROID)
#else
pthread_condattr_t cattr;
int iRet = -1;
iRet = pthread_condattr_init(cattr);
if (iRet != 0)
{
return;
}
pthread_mutex_init(s_mut, NULL);
pthread_condattr_setclock(cattr, CLOCK_MONOTONIC);
pthread_cond_init(s_cond, cattr);
pthread_condattr_destroy(cattr);
#endif
return;
}
void waitTimeout(void)
{
unsigned long long ullbefore = getSysTime();
unsigned long long ullafter = 0;
#if defined(ANDROID)
#if defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC) // 支持ANDROID下NDK的編譯,採用相對時間
struct timespec outtime;
memset(outtime, 0x00, sizeof(struct timespec ));
clock_gettime(CLOCK_MONOTONIC, outtime);
outtime.tv_sec += 2;
pthread_mutex_lock(s_mut);
pthread_cond_timedwait_monotonic(s_cond,s_mut, outtime);
pthread_mutex_unlock(s_mut);
ullafter = getSysTime();
printf("####01 interval[%lld] ms\n", ullafter - ullbefore);
#else //支持ANDROID下NDK的編譯,採用絕對時間
struct timeval now;
struct itmespec outtime;
gettimeofday(now, NULL);
outtime.tv_sec = now..tv_sec + 3;
outtime.tv_nsec = now.tv_usec * 1000;
pthread_mutex_lock(s_mut);
pthread_cond_timedwait(s_cond, s_mut, outtime);
pthread_mutex_unlock(s_mut);
ullafter = getSysTime();
printf("####02 interval[%lld] ms\n", ullafter - ullbefore);
#endif
#else // 支持LINUX下的編譯,採用絕對時間
struct timespec outtime;
memset(outtime, 0x00, sizeof(struct timespec ));
clock_gettime(CLOCK_MONOTONIC, outtime);
outtime.tv_sec += 4;
pthread_mutex_lock(s_mut);
pthread_cond_timedwait(s_cond, s_mut, outtime);
pthread_mutex_unlock(s_mut);
ullafter = getSysTime();
printf("####03 interval[%lld] ms\n", ullafter - ullbefore);
#endif
return;
}
unsigned long long getSysTime(void)
{
unsigned long long milliseconds = 0;
struct tms t_tmsTime;
clock_t t_CurTime;
static int s_clks_per_sec = 0;
if (s_clks_per_sec == 0)
{
s_clks_per_sec = sysconf(_SC_CLK_TCK);
}
if (s_clks_per_sec == 0)
{
return 0;
}
t_CurTime = times(t_tmsTime);
if (1000 % s_clks_per_sec == 0)
{
milliseconds = (1000 /s_clks_per_sec)*(unsigned long long )t_CurTime;//換算成毫秒
}
else
{
milliseconds = 1000 * (unsigned long long )t_CurTime/s_clks_per_sec;//換算成毫秒
}
return milliseconds;
}
int main(void)
{
PthreadAttr_Init();
waitTimeout();
return 0;
}
編譯命令:
gcc test_ptthrad_conf_timewait_monotonic.c -o test_ptthrad_conf_timewait_monotonic -lpthread -lrt
linux下的測試結果:
####03 interval[4010] ms