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