Android中提供的HttpURLConnection和HttpClient接口能夠用來開發HTTP程序。如下是本人在學習中的總結與概括。
1. HttpURLConnection接口
首先須要明確的是,Http通訊中的POST和GET請求方式的不一樣。GET能夠得到靜態頁面,也能夠把參數放在URL字符串後面,傳遞給服務器。而POST方法的參數是放在Http請求中。所以,在編程以前,應當首先明確使用的請求方法,而後再根據所使用的方式選擇相應的編程方式。
HttpURLConnection是繼承於URLConnection類,兩者都是抽象類。其對象主要經過URL的openConnection方法得到。建立方法以下代碼所示:android
- URL url = new URL("http://www.51cto.com/index.jsp?par=123456");
- HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();
|
經過如下方法能夠對請求的屬性進行一些設置,以下所示:編程
-
- urlConn.setDoOutput(true);
- urlConn.setDoInput(true);
-
- urlConn.setRequestMethod("POST");
-
- urlConn.setUseCaches(false);
-
- urlConn.disConnection();
-
|
HttpURLConnection默認使用GET方式,例以下面代碼所示:
-
- HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
-
- InputStreamReader in = new InputStreamReader(urlConn.getInputStream());
-
- BufferedReader buffer = new BufferedReader(in);
- String inputLine = null;
-
- while (((inputLine = buffer.readLine()) != null))
- {
-
- resultData += inputLine + "\n";
- }
-
- in.close();
-
- urlConn.disconnect();
|
若是須要使用POST方式,則須要setRequestMethod設置。代碼以下:
- String httpUrl = "http://192.168.1.110:8080/httpget.jsp";
-
- String resultData = "";
- URL url = null;
- try
- {
-
- url = new URL(httpUrl);
- }
- catch (MalformedURLException e)
- {
- Log.e(DEBUG_TAG, "MalformedURLException");
- }
- if (url != null)
- {
- try
- {
-
- HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
-
- urlConn.setDoOutput(true);
- urlConn.setDoInput(true);
-
- urlConn.setRequestMethod("POST");
-
- urlConn.setUseCaches(false);
- urlConn.setInstanceFollowRedirects(true);
-
- urlConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
-
-
- urlConn.connect();
-
- DataOutputStream out = new DataOutputStream(urlConn.getOutputStream());
-
- String content = "par=" + URLEncoder.encode("ABCDEFG", "gb2312");
-
- out.writeBytes(content);
-
- out.flush();
- out.close();
|
2. HttpClient接口
使用Apache提供的HttpClient接口一樣能夠進行HTTP操做。
對於GET和POST請求方法的操做有所不一樣。GET方法的操做代碼示例以下:
-
- String httpUrl = "http://192.168.1.110:8080/httpget.jsp?par=HttpClient_android_Get";
-
- HttpGet httpRequest = new HttpGet(httpUrl);
-
- HttpClient httpclient = new DefaultHttpClient();
-
- HttpResponse httpResponse = httpclient.execute(httpRequest);
-
- if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
- {
-
- String strResult = EntityUtils.toString(httpResponse.getEntity());
- mTextView.setText(strResult);
- }
- else
- {
- mTextView.setText("請求錯誤!");
- }
- }
|
使用POST方法進行參數傳遞時,須要使用NameValuePair來保存要傳遞的參數。,另外,還須要設置所使用的字符集。代碼以下所示:
-
- String httpUrl = "http://192.168.1.110:8080/httpget.jsp";
-
- HttpPost httpRequest = new HttpPost(httpUrl);
-
- List<NameValuePair> params = new ArrayList<NameValuePair>();
-
- params.add(new BasicNameValuePair("par", "HttpClient_android_Post"));
-
- HttpEntity httpentity = new UrlEncodedFormEntity(params, "gb2312");
-
- httpRequest.setEntity(httpentity);
-
- HttpClient httpclient = new DefaultHttpClient();
-
- HttpResponse httpResponse = httpclient.execute(httpRequest);
-
- if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
- {
-
- String strResult = EntityUtils.toString(httpResponse.getEntity());
- mTextView.setText(strResult);
- }
- else
- {
- mTextView.setText("請求錯誤!");
- }
- }
|
HttpClient其實是對Java提供方法的一些封裝,在HttpURLConnection中的輸入輸出流操做,在這個接口中被統一封裝成了HttpPost(HttpGet)和HttpResponse,這樣,就減小了操做的繁瑣性。緩存
另外,在使用POST方式進行傳輸時,須要進行字符編碼。服務器