android httpclient请求问题
/***数据请求**@paramurl*@return*/publicstaticStringqueryStringForPost(Stringurl){//String...
/*** 数据请求* * @param url* @return*/public static String queryStringForPost(String url) {// String s_url = "";String realurl = s_url + url;HttpPost request = HttpUtil.getHttpPost(realurl);String result = null;try {HttpResponse response = HttpUtil.getHttpResponse(request);if (response.getStatusLine().getStatusCode() == 200) {result = EntityUtils.toString(response.getEntity());return result;}} catch (ClientProtocolException e) {e.printStackTrace();result = "网络异常!";return result;} catch (IOException e) {e.printStackTrace();result = "网络异常!";return result;} return null;}// 异常的方法public static HttpResponse getHttpResponse(HttpPost request)throws ClientProtocolException, IOException {HttpResponse response = new DefaultHttpClient().execute(request);return response;}/*** 此应用所有网络连接使用一个httpClient* * @return*/private HttpClient createHttpClient() {Log.i(APP, "CREATE HTTPCLIENT");HttpParams httpParams = new BasicHttpParams();HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);HttpProtocolParams.setContentCharset(httpParams,HTTP.DEFAULT_CONTENT_CHARSET);HttpProtocolParams.setUseExpectContinue(httpParams, true);/* 连接超时 */HttpConnectionParams.setConnectionTimeout(httpParams, 1000);/* 请求超时 */HttpConnectionParams.setSoTimeout(httpParams, 4000);SchemeRegistry schemeRegistry = new SchemeRegistry();schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));// 关键代码--事关线程安全。ClientConnectionManager clientConnectionManager = new ThreadSafeClientConnManager(httpParams, schemeRegistry);return new DefaultHttpClient(clientConnectionManager, httpParams);}public HttpClient getHttpClient() {return httpClient;}@Overridepublic void onLowMemory() {super.onLowMemory();shutdownHttpClient();}@Overridepublic void onTerminate() {super.onTerminate();shutdownHttpClient();}private static void shutdownHttpClient() {if (httpClient != null && httpClient.getConnectionManager() != null) {httpClient.getConnectionManager().shutdown();}}我使用的是httpclient请求直接调用web端action方法,是不是数据请求完后自动回释放掉连接,还是每次需要我再调用下shutdownHttpClient()方法。
展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询