Restful API Service

REST api vs REST Webservice vs RESTFul web service

A RESTful web service is the implementation of the REST API (Application Programmable Interface) or the REST spec. In this case you are also creating an API with your endpoints that you create. In a sense they are actually the same thing and are just two terms that are interchangeable in the context of your question. The wording may be a little confusing but tl;dr; RESTful Web Service == REST API == REST Web Service for this situation

Outside of the context of the OP's question an API is not the same as a Web Service. An API can mean any interface or library used to interact with anything from code. This can be a smart dishwasher API or Web Sockets or cell phone SMS or even jQuery. In that same sense a Web Service is just a kind of API that provides information over the web.

What is RESTful programming?

An architectural style called REST (Representational State Transfer) advocates that web applications should use HTTP as it was originally envisioned. Lookups should use GET requests. PUT, POST, and DELETE requests should be used for mutation, creation, and deletion respectively.

REST proponents tend to favor URLs, such as

http://myserver.com/catalog/item/1729

but the REST architecture does not require these "pretty URLs". A GET request with a parameter

http://myserver.com/catalog?item=1729

is every bit as RESTful.

Keep in mind that GET requests should never be used for updating information. For example, a GET request for adding an item to a cart

http://myserver.com/addToCart?cart=314159&item=1729

would not be appropriate. GET requests should be idempotent. That is, issuing a request twice should be no different from issuing it once. That's what makes the requests cacheable. An "add to cart" request is not idempotent—issuing it twice adds two copies of the item to the cart. A POST request is clearly appropriate in this context. Thus, even a RESTful web application needs its share of POST requests.

This is taken from the excellent book Core JavaServer faces book by David M. Geary.

What is difference between REST and API?

REST is a type of API. Not all APIs are REST, but all REST services are APIs.

API is a very broad term. Generally it's how one piece of code talks to another. In web development API often refers to the way in which we retrieve information from an online service. The API documentation will give you a list of URLs, query parameters and other information on how to make a request from the API, and inform you what sort of response will be given for each query.

REST is a set of rules/standards/guidelines for how to build a web API. Since there are many ways to do so, having an agreed upon system of structuring an API saves time in making decisions when building one, and saves time in understanding how to use one.

Other popular API paradigms include SOAP and GraphQL.

Note that the above attempts to answer the question in regards to how the terms are commonly used in web development. Roman Vottner has offered a different answer below which offers good insights into the original definition of the term REST with more technical precision than I have provided here.

Restful API service

If your service is going to be part of you application then you are making it way more complex than it needs to be. Since you have a simple use case of getting some data from a RESTful Web Service, you should look into ResultReceiver and IntentService.

This Service + ResultReceiver pattern works by starting or binding to the service with startService() when you want to do some action. You can specify the operation to perform and pass in your ResultReceiver (the activity) through the extras in the Intent.

In the service you implement onHandleIntent to do the operation that is specified in the Intent. When the operation is completed you use the passed in ResultReceiver to send a message back to the Activity at which point onReceiveResult will be called.

So for example, you want to pull some data from your Web Service.

  1. You create the intent and call startService.
  2. The operation in the service starts and it sends the activity a message saying it started
  3. The activity processes the message and shows a progress.
  4. The service finishes the operation and sends some data back to your activity.
  5. Your activity processes the data and puts in in a list view
  6. The service sends you a message saying that it is done, and it kills itself.
  7. The activity gets the finish message and hides the progress dialog.

I know you mentioned you didn't want a code base but the open source Google I/O 2010 app uses a service in this way I am describing.

Updated to add sample code:

The activity.

public class HomeActivity extends Activity implements MyResultReceiver.Receiver {

public MyResultReceiver mReceiver;

public void onCreate(Bundle savedInstanceState) {
mReceiver = new MyResultReceiver(new Handler());
mReceiver.setReceiver(this);
...
final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, QueryService.class);
intent.putExtra("receiver", mReceiver);
intent.putExtra("command", "query");
startService(intent);
}

public void onPause() {
mReceiver.setReceiver(null); // clear receiver so no leaks.
}

public void onReceiveResult(int resultCode, Bundle resultData) {
switch (resultCode) {
case RUNNING:
//show progress
break;
case FINISHED:
List results = resultData.getParcelableList("results");
// do something interesting
// hide progress
break;
case ERROR:
// handle the error;
break;
}
}

The Service:

public class QueryService extends IntentService {
protected void onHandleIntent(Intent intent) {
final ResultReceiver receiver = intent.getParcelableExtra("receiver");
String command = intent.getStringExtra("command");
Bundle b = new Bundle();
if(command.equals("query") {
receiver.send(STATUS_RUNNING, Bundle.EMPTY);
try {
// get some data or something
b.putParcelableArrayList("results", results);
receiver.send(STATUS_FINISHED, b)
} catch(Exception e) {
b.putString(Intent.EXTRA_TEXT, e.toString());
receiver.send(STATUS_ERROR, b);
}
}
}
}

ResultReceiver extension - edited about to implement MyResultReceiver.Receiver

public class MyResultReceiver implements ResultReceiver {
private Receiver mReceiver;

public MyResultReceiver(Handler handler) {
super(handler);
}

public void setReceiver(Receiver receiver) {
mReceiver = receiver;
}

public interface Receiver {
public void onReceiveResult(int resultCode, Bundle resultData);
}

@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
if (mReceiver != null) {
mReceiver.onReceiveResult(resultCode, resultData);
}
}
}

RESTful API vs Web Service API

Has been asked many times, e.g.

  • Why would one use REST instead of SOAP based services?
  • WSDL vs REST Pros and Cons
  • SOAP or REST for Web Services?


Related Topics



Leave a reply



Submit