HttpURLConnection是繼承於URLConnection類,兩者都是抽象類。其對象主要經過URL的openConnection方法得到。建立方法以下代碼所示:java
URL url = new URL("http://www.51cto.com/index.jsp?par=123456"); HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();
經過如下方法能夠對請求的屬性進行一些設置,以下所示:
//設置輸入和輸出流 urlConn.setDoOutput(true); urlConn.setDoInput(true); //設置請求方式爲POST urlConn.setRequestMethod("POST"); //POST請求不能使用緩存 urlConn.setUseCaches(false); //關閉鏈接 urlConn.disConnection();
HttpURLConnection默認使用GET方式,例以下面代碼所示:
//使用HttpURLConnection打開鏈接 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); //獲得讀取的內容(流) InputStreamReader in = new InputStreamReader(urlConn.getInputStream()); // 爲輸出建立BufferedReader BufferedReader buffer = new BufferedReader(in); String inputLine = null; //使用循環來讀取得到的數據 while (((inputLine = buffer.readLine()) != null)) { //咱們在每一行後面加上一個"\n"來換行 resultData += inputLine + "\n"; } //關閉InputStreamReader in.close(); //關閉http鏈接 urlConn.disconnect();
若是須要使用POST方式,則須要setRequestMethod設置。代碼以下:
String httpUrl = "http://192.168.1.110:8080/httpget.jsp"; //得到的數據 String resultData = ""; URL url = null; try { //構造一個URL對象 url = new URL(httpUrl); } catch (MalformedURLException e) { Log.e(DEBUG_TAG, "MalformedURLException"); } if (url != null) { try { // 使用HttpURLConnection打開鏈接 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); //由於這個是post請求,設立須要設置爲true urlConn.setDoOutput(true); urlConn.setDoInput(true); // 設置以POST方式 urlConn.setRequestMethod("POST"); // Post 請求不能使用緩存 urlConn.setUseCaches(false); urlConn.setInstanceFollowRedirects(true); // 配置本次鏈接的Content-type,配置爲application/x-www-form-urlencoded的 urlConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); // 鏈接,從postUrl.openConnection()至此的配置必需要在connect以前完成, // 要注意的是connection.getOutputStream會隱含的進行connect。 urlConn.connect(); //DataOutputStream流 DataOutputStream out = new DataOutputStream(urlConn.getOutputStream()); //要上傳的參數 String content = "par=" + URLEncoder.encode("ABCDEFG", "gb2312"); //將要上傳的內容寫入流中 out.writeBytes(content); //刷新、關閉 out.flush(); out.close();
2. HttpClient接口
// http地址 String httpUrl = "http://192.168.1.110:8080/httpget.jsp?par=HttpClient_android_Get"; //HttpGet鏈接對象 HttpGet httpRequest = new HttpGet(httpUrl); //取得HttpClient對象 HttpClient httpclient = new DefaultHttpClient(); //請求HttpClient,取得HttpResponse 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來保存要傳遞的參數。,另外,還須要設置所使用的字符集。代碼以下所示:
// http地址 String httpUrl = "http://192.168.1.110:8080/httpget.jsp"; //HttpPost鏈接對象 HttpPost httpRequest = new HttpPost(httpUrl); //使用NameValuePair來保存要傳遞的Post參數 List<NameValuePair> params = new ArrayList<NameValuePair>(); //添加要傳遞的參數 params.add(new BasicNameValuePair("par", "HttpClient_android_Post")); //設置字符集 HttpEntity httpentity = new UrlEncodedFormEntity(params, "gb2312"); //請求httpRequest httpRequest.setEntity(httpentity); //取得默認的HttpClient HttpClient httpclient = new DefaultHttpClient(); //取得HttpResponse HttpResponse httpResponse = httpclient.execute(httpRequest); //HttpStatus.SC_OK表示鏈接成功 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,這樣,就減小了操做的繁瑣性。android
另外,在使用POST方式進行傳輸時,須要進行字符編碼。編程