Share an Android and Java tool that calls RESTful Web services resting

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.