Share a Resting tool for Android and java to call RESTful Web Services

Source: Internet
Author: User

Share a Resting tool for Android and java to call RESTful Web Services
Share a Resting tool for Android and java to call RESTful Web Services

 

When we call a Web service, the ultimate goal is to get an HTTP Response and convert it into a value object that will be presented in the application. Resting can be used to implement this function.
Resting, a lightweight REST framework in Java, can be used to call a RESTful Web service and convert it into a response to a Java object customized by a client application. Thanks to its simplicity, resting is suitable for Android and other handheld devices.

 

Resting Objectives
• Expose simple get (), post (), put (), and delete () Methods to consume the REST Service
• Supports all common MIME types such as JSON, XML, ATOM, and YAML
• Enable HTTP and HTTPS (SSL) calls for restful Web Services
• Supports basic authentication
• Support proxy
• Support any complicated data grouping and grouping during the transformation process
• Supports customization of representativeness in the REST request set
• Lightweight, easy to operate, and fast. It is very suitable for Android systems.

 

1. Import the corresponding 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 RequestParams, call the static method Resting. getByJSON, and convert it to the corresponding object:


Public void testGetByJSON (){
System. out. println (testGetByJSON -----------------------------);
RequestParams jsonParams = new JSONRequestParams ();
JsonParams. add (key, fdb3cda-a8d22d174cafeadc6d4c1405b08d5609 );
Try {
List 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;
}
}

 

3. Static Methods in Resting support: get (), post (), put (), and delete ()


GetbyJSON, getbyXML, getbyATOM, and getbyYAML are supported.
For example, see the resting.zip esting estingsrc estcomgoogle esting in the source code.
Other Wiki pages that briefly describe how to use this component (you can open it using eclipse/vs/notepad ++): resting.zip wiki

 

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.