當我們調用Web服務,往往是最終目标是取HTTP響應,将其轉化為将在應用中呈現的值對象。Resting可以用來實作這一功能。
Resting,在Java的一個輕量級的REST架構,可用于調用一個RESTful Web服務,并轉換成響應來自用戶端應用程式定制的Java對象。由于它的簡單,resting是适合Android等手持裝置。
resting目标
•暴露簡單的get(),post(),put()和delete()方法來消費REST服務
•支援像JSON,XML,ATOM和YAML所有常用的MIME類型
•啟用的REST風格的Web服務HTTP和HTTPS(SSL)調用
•支援基本身份驗證
•支援代理
•支援轉型過程中任意複雜的編組資料和解組
•支援在REST請求集合定制代表性
•輕量級,操作簡單,快捷。非常适用于Android系統。
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import com.google.resting.Resting;
import com.google.resting.RestingBuilder;
import com.google.resting.atom.AtomFeed;
import com.google.resting.component.Alias;
import com.google.resting.component.EncodingTypes;
import com.google.resting.component.RequestParams;
import com.google.resting.component.Verb;
import com.google.resting.component.content.ContentType;
import com.google.resting.component.impl.BasicRequestParams;
import com.google.resting.component.impl.ServiceResponse;
import com.google.resting.component.impl.json.JSONAlias;
import com.google.resting.component.impl.json.JSONRequestParams;
import com.google.resting.component.impl.xml.XMLAlias;
import com.google.resting.transform.impl.JSONTransformer;
import com.google.resting.transform.impl.XMLTransformer;
import com.google.resting.util.ReflectionUtil;
public void testGetByJSON() {
System.out.println("\ntestGetByJSON\n-----------------------------");
RequestParams jsonParams = new JSONRequestParams();
jsonParams.add("key", "fdb3c385a8d22d174cafeadc6d4c1405b08d5609");
try {
System.out.println("[RestingTest::getByJSON] The product detail is "+products.get(0).toString());
} catch (Exception e) {
e.printStackTrace();
}
}
public class Product {
private int productId;
public int getProductId(){
return productId;
public String toString(){
return "Product id="+productId;
}
同時支援getbyJSON,getbyXML,getbyATOM和getbyYAML
例子請看源碼下載下傳裡的resting.zip\resting\resting\src\test\com\google\resting
另外一些簡單介紹這個元件如何使用的Wiki頁面(可以使用eclipse/vs/notepad++等打開):resting.zip\wiki