天天看點

android發送http請求—-URLConnection、HttpURLConnection的使用

1. 使用 标準Java接口: 設計的類: java.net.*

基本步驟:

1) 建立 URL 以及 URLConnection / HttpURLConnection 對象

2) 設定連接配接參數

3) 連接配接到伺服器

4) 向伺服器寫資料

5)從伺服器讀取資料

例:

try {// 建立一個 URL 對象URL url = new URL(your_url);// 建立一個 URL 連接配接,如果有代理的話可以指定一個代理。URLConnection connection = url.openConnection(Proxy_yours);// 對于 HTTP 連接配接可以直接轉換成 HttpURLConnection,這樣就可以使用一些 HTTP 連接配接特定的方法,如 setRequestMethod() 等://HttpURLConnection connection =// (HttpURLConnection)url.openConnection(Proxy_yours);// 在開始和伺服器連接配接之前,可能需要設定一些網絡參數connection.setConnectTimeout(10000);connection.addRequestProperty(“User-Agent”,“J2me/MIDP2.0″);// 連接配接到伺服器connection.connect();// 與伺服器互動:OutputStream outStream = connection.getOutputStream();ObjectOutputStream objOutput = new ObjectOutputStream(outStream);objOutput.writeObject(new String(“this is a string…”));objOutput.flush();InputStream in = connection.getInputStream();// 處理資料…} catch (Exception e) {// 網絡讀寫操作往往會産生一些異常,是以在具體編寫網絡應用時// 最好捕捉每一個具體以采取相應措施}

2. 使用 apache 接口:

Apache HttpClient 是一個開源項目,彌補了 java.net.* 靈活性不足的缺點, 支援用戶端的HTTP程式設計.

使用的類包括: org.apache.http.*

步驟:

1) 建立 HttpClient 以及 GetMethod / PostMethod, HttpRequest 等對象;

2) 設定連接配接參數;

3) 執行 HTTP 操作;

4) 處理伺服器傳回結果.

例:

try {// 建立 HttpParams 以用來設定 HTTP 參數(這一部分不是必需的)HttpParams params = new BasicHttpParams();// 設定連接配接逾時和 Socket 逾時,以及 Socket 緩存大小:HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);HttpConnectionParams.setSoTimeout(params, 20 * 1000);HttpConnectionParams.setSocketBufferSize(params, 8192);// 設定重定向,預設為 true:HttpClientParams.setRedirecting(params, true);// 設定 user agent:HttpProtocolParams.setUserAgent(params, userAgent);// 建立一個 HttpClient 執行個體:// 注意: HttpClient httpClient = new HttpClient(); 是Commons HttpClient中的用法,// 在 Android 1.5 中我們需要使用 Apache 的預設實作 DefaultHttpClient.DefaultHttpClient httpClient = new DefaultHttpClient(params);// 建立 HttpGet 方法,該方法會自動處理 URL 位址的重定向:HttpGet httpGet = new HttpGet (“http://www.test_test.com/”);//執行此方法:HttpResponse response = client.execute(httpGet);if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {// 錯誤處理,例如可以在該請求正常結束前将其中斷:httpGet.abort();}// 讀取更多資訊Header[] headers = response.getHeaders();HttpEntity entity = response.getEntity();Header header = response.getFirstHeader(“Content-Type”);} catch (Exception ee) {// …} finally {// 釋放連接配接:client.getConnectionManager().shutdown();}以下例子以 HttpGet 方式通過代理通路 HTTPS 網站:try {HttpClient httpClient = new HttpClient();// 設定認證的資料: httpClient好像沒有方法getCredentialsProvider()??httpClient.getCredentialsProvider().setCredentials(new AuthScope(“your_auth_host”, 80, “your_realm”),new UsernamePasswordCredentials(“username”, “password”));// 設定伺服器位址,端口,通路協定:HttpHost targetHost = new HttpHost(“www.verisign.com”, 443, “https”);// 設定代理:HttpHost proxy = new HttpHost(“192.168.1.1″, 80);httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);// 建立一個 HttpGet 執行個體:HttpGet httpGet = new HttpGet(“/a/b/c”);// 連接配接伺服器并擷取應答資料:HttpResponse response = httpClient.execute(targetHost, httpGet);// 讀取應答資料:int statusCode = response.getStatusLine().getStatusCode();Header[] headers = response.getHeaders();HttpEntity entity = response.getEntity();// …} catch (Exception ee) {// …}

3. 使用 android 接口:

類android.net.http.* 實際上是通過對 Apache 的 HttpClient 的封裝來實作的一個 HTTP 程式設計接口,同時還提供了 HTTP 請求隊列管理、以及 HTTP 連接配接池管理,以提高并發請求情況下(如轉載網頁時)的處理效率,除此之外還有網絡狀态監視等接口。

例:(class AndroidHttpClient : Since Android API level

按 Ctrl+C 複制代碼

try {

AndroidHttpClient client =

AndroidHttpClient.newInstance(“user_agent__my_mobile_browser”);

// 建立 HttpGet 方法,該方法會自動處理 URL 位址的重定向:

HttpGet httpGet = new HttpGet (“http://www.test_test.com/”);

HttpResponse response = client.execute(httpGet);

if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {

// 錯誤處理…

}

//…

// 關閉連接配接:

client.close();

} catch (Exception ee) {

//…

}

A Comparison of java.net.URLConnection and HTTPClient

Since java.net.URLConnection and HTTPClient have overlappingfunctionalities, the question arises of why would you use HTTPClient.Here are a few of the capabilites and tradeoffs.

HttpURLConnection與HttpClient的差別

1.概念

HTTP 協定可能是現在 Internet 上使用得最多、最重要的協定了,越來越多的 Java 應用程式需要直接通過 HTTP 協定來通路網絡資源。在 JDK 的 java.net 包中已經提供了通路 HTTP 協定的基本功能:HttpURLConnection。但是對于大部分應用程式來說,JDK 庫本身提供的功能還不夠豐富和靈活。

除此之外,在Android中,androidSDK中內建了Apache的HttpClient子產品,用來提供高效的、最新的、功能豐富的支援 HTTP 協定工具包,并且它支援 HTTP 協定最新的版本和建議。使用HttpClient可以快速開發出功能強大的Http程式。

2.差別

HttpClient是個很不錯的開源架構,封裝了通路http的請求頭,參數,内容體,響應等等,

HttpURLConnection是java的标準類,什麼都沒封裝,用起來太原始,不友善,比如重通路的自定義,以及一些進階功能等。

3.案例

URLConnection

String urlAddress = "http://192.168.1.102:8080/AndroidServer/login.do"; URL url; HttpURLConnection uRLConnection; public UrlConnectionToServer(){ }

//向伺服器發送get請求public String doGet(String username,String password){ String getUrl = urlAddress + "?username="+username+"&password="+password; try { url = new URL(getUrl); uRLConnection = (HttpURLConnection)url.openConnection(); InputStream is = uRLConnection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String response = ""; String readLine = null; while((readLine =br.readLine()) != null){ //response = br.readLine(); response = response + readLine; } is.close(); br.close(); uRLConnection.disconnect(); return response; } catch (MalformedURLException e) { e.printStackTrace(); returnnull; } catch (IOException e) { e.printStackTrace(); returnnull; } }

//向伺服器發送post請求public String doPost(String username,String password){ try { url = new URL(urlAddress); uRLConnection = (HttpURLConnection)url.openConnection(); uRLConnection.setDoInput(true); uRLConnection.setDoOutput(true); uRLConnection.setRequestMethod("POST"); uRLConnection.setUseCaches(false); uRLConnection.setInstanceFollowRedirects(false); uRLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); uRLConnection.connect(); DataOutputStream out = new DataOutputStream(uRLConnection.getOutputStream()); String content = "username="+username+"&password="+password; out.writeBytes(content); out.flush(); out.close(); InputStream is = uRLConnection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String response = ""; String readLine = null; while((readLine =br.readLine()) != null){ //response = br.readLine(); response = response + readLine; } is.close(); br.close(); uRLConnection.disconnect(); return response; } catch (MalformedURLException e) { e.printStackTrace(); returnnull; } catch (IOException e) { e.printStackTrace(); returnnull; } }

HTTPClient

String urlAddress = "http://192.168.1.102:8080/qualityserver/login.do"; public HttpClientServer(){ } public String doGet(String username,String password){ String getUrl = urlAddress + "?username="+username+"&password="+password; HttpGet httpGet = new HttpGet(getUrl); HttpParams hp = httpGet.getParams(); hp.getParameter("true"); //hp. //httpGet.setp HttpClient hc = new DefaultHttpClient(); try { HttpResponse ht = hc.execute(httpGet); if(ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ HttpEntity he = ht.getEntity(); InputStream is = he.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String response = ""; String readLine = null; while((readLine =br.readLine()) != null){ //response = br.readLine(); response = response + readLine; } is.close(); br.close(); //String str = EntityUtils.toString(he); System.out.println("========="+response); return response; }else{ return "error"; } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); return "exception"; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return "exception"; } } public String doPost(String username,String password){ //String getUrl = urlAddress + "?username="+username+"&password="+password; HttpPost httpPost = new HttpPost(urlAddress); List params = new ArrayList(); NameValuePair pair1 = new BasicNameValuePair("username", username); NameValuePair pair2 = new BasicNameValuePair("password", password); params.add(pair1); params.add(pair2); HttpEntity he; try { he = new UrlEncodedFormEntity(params, "gbk"); httpPost.setEntity(he); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } HttpClient hc = new DefaultHttpClient(); try { HttpResponse ht = hc.execute(httpPost); //連接配接成功 if(ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ HttpEntity het = ht.getEntity(); InputStream is = het.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String response = ""; String readLine = null; while((readLine =br.readLine()) != null){ //response = br.readLine(); response = response + readLine; } is.close(); br.close(); //String str = EntityUtils.toString(he); System.out.println("=========&&"+response); return response; }else{ return "error"; } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); return "exception"; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return "exception"; } }

servlet端json轉化:

resp.setContentType("text/json"); resp.setCharacterEncoding("UTF-8"); toDo = new ToDo(); List<UserBean> list = new ArrayList<UserBean>(); list = toDo.queryUsers(mySession); String body; //設定JSON JSONArray array = new JSONArray(); for(UserBean bean : list) { JSONObject obj = new JSONObject(); try { obj.put("username", bean.getUserName()); obj.put("password", bean.getPassWord()); }catch(Exception e){} array.add(obj); } pw.write(array.toString()); System.out.println(array.toString());

android端接收:

String urlAddress = "http://192.168.1.102:8080/qualityserver/result.do"; String body = getContent(urlAddress); JSONArray array = new JSONArray(body); for(int i=0;i<array.length();i++) { obj = array.getJSONObject(i); sb.append("使用者名:").append(obj.getString("username")).append("\t"); sb.append("密碼:").append(obj.getString("password")).append("\n"); HashMap<String, Object> map = new HashMap<String, Object>(); try { userName = obj.getString("username"); passWord = obj.getString("password"); } catch (JSONException e) { e.printStackTrace(); } map.put("username", userName); map.put("password", passWord); listItem.add(map); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if(sb!=null) { showResult.setText("使用者名和密碼資訊:"); showResult.setTextSize(20); } else extracted(); //設定adapter SimpleAdapter simple = new SimpleAdapter(this,listItem, android.R.layout.simple_list_item_2, new String[]{"username","password"}, newint[]{android.R.id.text1,android.R.id.text2}); listResult.setAdapter(simple); listResult.setOnItemClickListener(new OnItemClickListener() { @Override publicvoid onItemClick(AdapterView<?> parent, View view, int position, long id) { int positionId = (int) (id+1); Toast.makeText(MainActivity.this, "ID:"+positionId, Toast.LENGTH_LONG).show(); } }); } privatevoid extracted() { showResult.setText("沒有有效的資料!"); } //和伺服器連接配接 private String getContent(String url)throws Exception{ StringBuilder sb = new StringBuilder(); HttpClient client =new DefaultHttpClient(); HttpParams httpParams =client.getParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 3000); HttpConnectionParams.setSoTimeout(httpParams, 5000); HttpResponse response = client.execute(new HttpGet(url)); HttpEntity entity =response.getEntity(); if(entity !=null){ BufferedReader reader = new BufferedReader(new InputStreamReader (entity.getContent(),"UTF-8"),8192); String line =null; while ((line= reader.readLine())!=null){ sb.append(line +"\n"); } reader.close(); } return sb.toString(); }

URLConnection

HTTPClient

Proxies and SOCKS

Full support in Netscape browser, appletviewer, and applications (SOCKS: Version 4 only); no additional limitations from security policies.

Full support (SOCKS: Version 4 and 5); limited in applets however by security policies; in Netscape can't pick up the settings from the browser.

Authorization

Full support for Basic Authorization in Netscape (can use info given by the user for normal accesses outside of the applet); no support in appletviewer or applications.

Full support everywhere; however cannot access previously given info from Netscape, thereby possibly requesting the user to enter info (s)he has already given for a previous access. Also, you can add/implement additional authentication mechanisms yourself.

Methods

Only has GET and POST.

Has HEAD, GET, POST, PUT, DELETE, TRACE and OPTIONS, plus any arbitrary method.

Headers

Currently you can only set any request headers if you are doing a POST under Netscape; for GETs and the JDK you can't set any headers.

Under Netscape 3.0 you can read headers only if the resource was returned with a Content-length header; if no Content-length header was returned, or under previous versions of Netscape, or using the JDK no headers can be read.

Allows any arbitrary headers to be sent and received.

Automatic Redirection Handling

Yes.

Yes (as allowed by the HTTP/1.1 spec).

Persistent Connections

No support currently in JDK; under Netscape uses HTTP/1.0 Keep-Alive's.

Supports HTTP/1.0 Keep-Alive's and HTTP/1.1 persistence.

Pipelining of Requests

No.

Yes.

Can handle protocols other than HTTP

Theoretically; however only http is currently implemented.

No.

Can do HTTP over SSL (https)

Under Netscape, yes. Using Appletviewer or in an application, no.

No (not yet).

Source code available

No.

Yes.