2. REST API
A RESTful API takes advantage of HTTP methodologies.
â GET to retrieve a resource
â PUT to change the state of or update a resource, which
can be an object, file or block
â POST to create that resource
â DELETE to remove it.
3. Request method example
â GET /person â list person
â GET /person/id â list person with given id
â POST /person â create new person
â PUT /person/id â edit or update person with given id
â DELETE /person/id â delete person with given id
4. JSON
JSON (JavaScript Object Notation) is a lightweight
data-interchange format. It is easy for humans to read and
write.
5. JSON Structure
â A collection of name/value pairs. In various languages,
this is realized as an object, record, struct, dictionary,
hash table, keyed list, or associative array.
â An ordered list of values. In most languages, this is
realized as an array, vector, list, or sequence.
6.
7.
8.
9. Retrofit API Client
retrofit= new Retrofit.Builder()
.baseUrl("http://api.example.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
10. Retrofit API Interface
public interface ApiInterface {
@GET("place")
Call<List<Person>> doGetPlaceList();
}
Note: request url â http://api.example.com/place
11. // Api call
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<List<Place>> call = apiInterface.doGetPlaceList();
call.enqueue(new Callback<List<Place>>() {
@Override
public void onResponse(Call<List<Place>> call, Response<List<Place>> response) {
int statusCode = response.code();
Log.d("TAG", "code = " + statusCode);
List<Place> place = response.body();
adapter = new PlaceAdapter(place);
recyclerView.setAdapter(adapter);
}
@Override
public void onFailure(Call<List<Place>> call, Throwable t) {
Log.d("TAG", "" + t.getMessage());
}
});
API Call