Android開發中網絡請求

直接上代碼,post請求安全

public static String httpPost(String posturl,
			List<BasicNameValuePair> listParmas, final Context context) {
		activityContext = context;
		String res = "";
		/**
		 * 先建立鏈接
		 */
		HttpClient client = getHttpClient();
		/**
		 * 使用Post請求,傳入的Http地址正確
		 */
		HttpPost post = new HttpPost(posturl);

		try {
			if (listParmas.size() != 0) {
				post.setEntity(new UrlEncodedFormEntity(listParmas, "utf-8"));
			}
			post.setHeader("token", HttpUrlNew.TOAKEN);
			/**
			 * 開始執行post鏈接
			 */
			post.getParams().setParameter(
					HttpConnectionParams.CONNECTION_TIMEOUT, TIME_OUT);// 請求超時
			post.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
					TIME_OUT);// 讀取超時
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {// 表明請求數據成功
				HttpEntity entity = response.getEntity();
				res = EntityUtils.toString(entity);
			} else {
				HttpEntity entity = response.getEntity();
				res = EntityUtils.toString(entity);
				handler.sendEmptyMessage(Util.HTTP_CONNECTION_500);
			}
		} catch (ClientProtocolException e) {
			handler.sendEmptyMessage(Util.HTTP_CONNECTION_CLIENT_PROTOCOL_EXCEPTION);
			e.printStackTrace();
		} catch (ConnectTimeoutException e) {
			handler.sendEmptyMessage(Util.HTTP_CONNECTION_TIMEOUT);
			// e.printStackTrace();
		} catch (UnknownHostException e) {
			handler.sendEmptyMessage(Util.HTTP_CONNECTION_UNKONW_HOST);
		} catch (IOException e) {
			handler.sendEmptyMessage(Util.HTTP_CONNECTION_IO_EXCEPTION);
		}
		
		return res;

	}

get請求post

public static String httpGet(String posturl, Context context) {
		activityContext = context;
		String res = "";
		/**
		 * 先建立鏈接
		 */
		// HttpClient client = getHttpClient();
		HttpClient client = new DefaultHttpClient();
		/**
		 * 使用get請求,傳入的Http地址正確
		 */
		HttpGet get = new HttpGet(posturl);

		try {
			get.setHeader("token", HttpUrlNew.TOAKEN);
			/**
			 * 開始執行get鏈接
			 */
			get.getParams().setParameter(
					HttpConnectionParams.CONNECTION_TIMEOUT, TIME_OUT);// 請求超時
			get.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
					TIME_OUT);// 讀取超時

			HttpResponse response = client.execute(get);
			if (response.getStatusLine().getStatusCode() == 200) {// 表明請求數據成功
				HttpEntity entity = response.getEntity();
				res = EntityUtils.toString(entity);
			} else {
				handler.sendEmptyMessage(Util.HTTP_CONNECTION_500);
			}
		} catch (ClientProtocolException e) {
			handler.sendEmptyMessage(Util.HTTP_CONNECTION_CLIENT_PROTOCOL_EXCEPTION);
			e.printStackTrace();
		} catch (ConnectTimeoutException e) {
			handler.sendEmptyMessage(Util.HTTP_CONNECTION_TIMEOUT);
			// e.printStackTrace();
		} catch (UnknownHostException e) {
			handler.sendEmptyMessage(Util.HTTP_CONNECTION_UNKONW_HOST);
		} catch (IOException e) {
			handler.sendEmptyMessage(Util.HTTP_CONNECTION_IO_EXCEPTION);
		}
		return res;
	}

上面方法中用到的getHttpClient方法url

public static synchronized DefaultHttpClient getHttpClient() {
		try {
			if (defaultClient == null) {
				// Log.v(TAG, "->> httpClient is null ->> do getHttpClient");
				// 設置組件參數, HTTP協議的版本,1.1/1.0/0.9
				HttpParams params = new BasicHttpParams();
				HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
				HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
				HttpProtocolParams.setUseExpectContinue(params, true);

				// 設置鏈接超時時間
				int REQUEST_TIMEOUT = 15 * 1000; // 設置請求超時15秒鐘
				int SO_TIMEOUT = 15 * 1000; // 設置等待數據超時時間15秒鐘
				HttpConnectionParams.setConnectionTimeout(params,
						REQUEST_TIMEOUT);
				HttpConnectionParams.setSoTimeout(params, SO_TIMEOUT);
				ConnManagerParams.setTimeout(params, 1000); // 從鏈接池中取鏈接的超時時間

				// 設置訪問協議
				SchemeRegistry schreg = new SchemeRegistry();
				schreg.register(new Scheme("http", PlainSocketFactory
						.getSocketFactory(), 80));
				schreg.register(new Scheme("https", SSLSocketFactory
						.getSocketFactory(), 443));

				// 使用線程安全的鏈接管理來建立HttpClient
				ClientConnectionManager conMgr = new ThreadSafeClientConnManager(
						params, schreg);
				defaultClient = new DefaultHttpClient(conMgr, params);
			}
			return defaultClient;
		} catch (Exception e) {
			return null;
		}

	}

在這個方法中defaultClient 字段是DefaultHttpClient對象.線程

在請求以後經過handler將獲得的請求信息來更新頁面.這裏就再也不貼代碼了,code

相關文章
相關標籤/搜索