如果http调用失败或无法在android中访问,则终止或停止后台线程
我在下面做,但如果服务器不可访问或不可用,它不会在2-3秒后将控制权交还给线程。10-15秒后打印完成请求。你知道为什么吗如果http调用失败或无法在android中访问,则终止或停止后台线程,android,http,connection,request,blocking,Android,Http,Connection,Request,Blocking,我在下面做,但如果服务器不可访问或不可用,它不会在2-3秒后将控制权交还给线程。10-15秒后打印完成请求。你知道为什么吗 public JSONArray makeHttpRequest(String url) { // Making HTTP request try { HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds
public JSONArray makeHttpRequest(String url) {
// Making HTTP request
try {
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 = 2000;
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 httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
Log.d("error_trace", "Done making request");
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (Exception e) {
e.printStackTrace();
errorFlag = true;
}
这可能是由数据连接中的延迟引起的。看看通过无线连接和非无线连接是否有区别。不,基本上我关闭了无线和4G,然后看到控制在10秒以上后恢复到线程状态。