Share an Android and Java tool that calls RESTful Web services resting
When we invoke a Web service, it is often the final goal to take an HTTP response and convert it into a value object that will be rendered in the app. Resting can be used to implement this function.
Resting, a lightweight rest framework in Java, can be used to invoke a RESTful Web service and transform it to respond to Java objects customized from the client application. Thanks to its simplicity, resting is suitable for handheld devices such as Android.
Resting target
? Expose simple get (), post (), put () and delete () methods to consume rest services
Support for all commonly used MIME types like Json,xml,atom and Yaml
? Enable RESTful Web service HTTP and HTTPS (SSL) calls
? support for Basic authentication
? Support Agent
Support any complex marshalling data and reconciliation groups during the transformation process
? Support custom representation in rest request collections
? Lightweight, easy to operate, fast. Ideal for Android systems.
1. Import the appropriate Java package:
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;
2. Initialize the requestparams, call the static method Resting.getbyjson and turn it into the appropriate object:
public void Testgetbyjson () {
System.out.println ("\ntestgetbyjson\n-----------------------------");
Requestparams jsonparams = new Jsonrequestparams ();
Jsonparams.add ("Key", "fdb3c385a8d22d174cafeadc6d4c1405b08d5609");
try {
List<product> Products=resting.getbyjson ("http://api.zappos.com/Product/7515478 ", 80,jsonparams, Product.class, "Product");
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;
}
}
static method support in 3.Resting: Get (), post (), put () and delete ()
Supports both Getbyjson,getbyxml,getbyatom and Getbyyaml
For an example, see the resting.zip\resting\resting\src\test\com\google\resting in the source download.
Another wiki page that briefly describes how this component is used (can be opened using eclipse/vs/notepad++, etc.): Resting.zip\wiki
Initial source code: http://code.google.com/p/resting/
SOURCE Download: http://download.csdn.net/detail/yangzhenping/8398281
Share an Android and Java tool that calls RESTful Web services resting