android http鏈接阻塞超時問題

android http鏈接一般使用方式 java


URL url = new URL(path);
		HttpURLConnection conn = null;
		try {
			conn = (HttpURLConnection) url.openConnection();
			conn.setConnectTimeout(3000);
			conn.setReadTimeout(3000);
		} catch (IOException e) {
			Log.e(TAG, "openConnection() failed! url = " + url);
			e.printStackTrace();
			return false;
		}
		try {
			conn.setRequestMethod("GET");
			if (conn.getResponseCode() == 200) {
				InputStream xmlStream = conn.getInputStream();
				parserStates(xmlStream);
			} else {
				Log.e(TAG,
						"RequestMethod failed! code = "
								+ conn.getResponseCode());
			}
		} catch (ConnectTimeoutException e) {
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		if (conn != null) {
			conn.disconnect();
		}

在設置超時時間的方法中,設置超時的時間3000,在阻塞的getResponseCode方法中時間不許。 android

這樣採起org.apache.http.client.HttpClient方法 apache

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 500);
HttpConnectionParams.setSoTimeout(httpParameters, 500);

HttpGet httpget = new HttpGet(updateUrl.toURI());
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.setParams(httpParameters);

HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();

//download file.....
這種方法能夠準備的計算阻塞時間
相關文章
相關標籤/搜索