gpt4 book ai didi

Android - setSoTimeout 不工作

转载 作者:行者123 更新时间:2023-11-29 22:06:15 24 4
gpt4 key购买 nike

所以我遇到了一个不工作的套接字超时。我按照现有帖子给出的所有说明进行操作,但它仍然无法正常工作(我从未收到套接字超时异常)。这是我的代码:

AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
String location = params[0];

try {
HttpGet httpGet = new HttpGet(location);
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 = 0;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);

// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 2000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

DefaultHttpClient client = new DefaultHttpClient(httpParameters);
Log.d(this.getClass().getSimpleName(), "STARTING CLIENT!!! ");
HttpResponse response = client.execute(httpGet);
Log.d(this.getClass().getSimpleName(), "CLIENT CANCELLED!!! ");

if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
return new Scanner(out.toString()).useDelimiter("\\A").next();
} else {
return "";
}
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
}
}

@Override
protected void onPostExecute(String result) {
try {
// doStuff
} catch (Exception e) {
e.printStackTrace();
}
}
};
task.execute(location);

所以我应该在两秒后得到套接字超时异常,但客户端只是忽略了它。有帮助吗?

提前致谢


所有代码如下:

public void fetch(String location) {    
AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
String location = params[0];

try {
HttpGet httpGet = new HttpGet(location);
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 = 0;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 2000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

DefaultHttpClient client = new DefaultHttpClient(httpParameters);
Log.d(this.getClass().getSimpleName(), "STARTING CLIENT!!! ");
HttpResponse response = client.execute(httpGet);
Log.d(this.getClass().getSimpleName(), "CLIENT CANCELLED!!! ");

if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
return new Scanner(out.toString()).useDelimiter("\\A").next();
} else {
return "";
}
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
}
}

@Override
protected void onPostExecute(String result) {
try {
//doStuff
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
task.execute(location);
}

最佳答案

前段时间我遇到了类似的问题。我做了一些实验,发现当我在模拟器中运行我的应用程序时,超时不起作用,而当我在真实设备上运行它时,它确实起作用了。我用 DefaultHttpClientHttpUrlConnectionAndroidHttpClient 测试了它,所有三个都显示了相同的结果;在模拟器中大约 20 秒后出现 IOexception (UnknownHostException),而不管任何设置的超时。

Google 显示其他人也报告了超时问题:

没有一个建议的解决方案对我有用,所以我想唯一可靠的解决方案是自己管理超时。

关于Android - setSoTimeout 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10749835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com