OkHttp–支持SPDY協議的高效HTTP庫

Android爲咱們提供了兩種HTTP交互的方式: HttpURLConnection 和 Apache HTTP Client,雖然二者都支持HTTPS,流的上傳和下載,配置超時,IPv6和鏈接池,已足夠知足咱們各類HTTP請求的需求。但更高效的使用HTTP能夠讓您的應用運行更快、更節省流量。而OkHttp庫就是爲此而生。html

OkHttp是一個高效的HTTP庫:java

  • 支持 SPDY ,共享同一個Socket來處理同一個服務器的全部請求
  • 若是SPDY不可用,則經過鏈接池來減小請求延時
  • 無縫的支持GZIP來減小數據流量
  • 緩存響應數據來減小重複的網絡請求

 

會從不少經常使用的鏈接問題中自動恢復。若是您的服務器配置了多個IP地址,當第一個IP鏈接失敗的時候,OkHttp會自動嘗試下一個IP。OkHttp還處理了代理服務器問題和SSL握手失敗問題。android

使用 OkHttp 無需重寫您程序中的網絡代碼。OkHttp實現了幾乎和java.net.HttpURLConnection同樣的API。若是您用了 Apache HttpClient,則OkHttp也提供了一個對應的okhttp-apache 模塊。git

Examples

下面的示例請求一個URL並答應出返回內容字符.github

package com.squareup.okhttp.guide; import com.squareup.okhttp.OkHttpClient; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class GetExample { OkHttpClient client = new OkHttpClient(); void run() throws IOException { String result = get(new URL("https://raw.github.com/square/okhttp/master/README.md")); System.out.println(result); } String get(URL url) throws IOException { HttpURLConnection connection = client.open(url); InputStream in = null; try { // Read the response. in = connection.getInputStream(); byte[] response = readFully(in); return new String(response, "UTF-8"); } finally { if (in != null) in.close(); } } byte[] readFully(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; for (int count; (count = in.read(buffer)) != -1; ) { out.write(buffer, 0, count); } return out.toByteArray(); } public static void main(String[] args) throws IOException { new GetExample().run(); } }

下面的代碼經過Post發送數據到服務器:apache

package com.squareup.okhttp.guide; import com.squareup.okhttp.OkHttpClient; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class PostExample { OkHttpClient client = new OkHttpClient(); void run() throws IOException { byte[] body = bowlingJson("Jesse", "Jake").getBytes("UTF-8"); String result = post(new URL("http://www.roundsapp.com/post"), body); System.out.println(result); } String post(URL url, byte[] body) throws IOException { HttpURLConnection connection = client.open(url); OutputStream out = null; InputStream in = null; try { // Write the request. connection.setRequestMethod("POST"); out = connection.getOutputStream(); out.write(body); out.close(); // Read the response. if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { throw new IOException("Unexpected HTTP response: " + connection.getResponseCode() + " " + connection.getResponseMessage()); } in = connection.getInputStream(); return readFirstLine(in); } finally { // Clean up. if (out != null) out.close(); if (in != null) in.close(); } } String readFirstLine(InputStream in) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); return reader.readLine(); } String bowlingJson(String player1, String player2) { return "{'winCondition':'HIGH_SCORE'," + "'name':'Bowling'," + "'round':4," + "'lastSaved':1367702411696," + "'dateStarted':1367702378785," + "'players':[" + "{'name':'" + player1 + "','history':[10,8,6,7,8],'color':-13388315,'total':39}," + "{'name':'" + player2 + "','history':[6,10,5,10,10],'color':-48060,'total':41}" + "]}"; } public static void main(String[] args) throws IOException { new PostExample().run(); } }

 

 

參考:緩存

http://square.github.io/okhttp/服務器

http://android-developers.blogspot.com/2011/09/androids-http-clients.html網絡

相關文章
相關標籤/搜索