Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Web Service Integration
Anuchit Chalothorn
anoochit@gmail.com
REST API
A RESTful API takes advantage of HTTP methodologies.
● GET to retrieve a resource
● PUT to change the state of or...
Request method example
● GET /person → list person
● GET /person/id → list person with given id
● POST /person → create ne...
JSON
JSON (JavaScript Object Notation) is a lightweight
data-interchange format. It is easy for humans to read and
write.
JSON Structure
● A collection of name/value pairs. In various languages,
this is realized as an object, record, struct, di...
Retrofit API Client
retrofit= new Retrofit.Builder()
.baseUrl("http://api.example.com/")
.addConverterFactory(GsonConverte...
Retrofit API Interface
public interface ApiInterface {
@GET("place")
Call<List<Person>> doGetPlaceList();
}
Note: request ...
// Api call
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<List<Place>> call = apiInte...
Picasso
Picasso.with(context)
.load(image_url)
.error(R.drawable.placeholder_error)
.placeholder(R.drawable.placeholder)
....
Build.gradle
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.square...
Tools
● Advance REST Client
● JSONView
● jsonlint.com
● jsonschema2pojo.org
Codelab: Get data from JSON url
JSON url
https://raw.githubusercontent.com/anoochit/j
sondata/master/data02.json
Codelab: Get data from REST API
REST API
https://warm-ridge-87805.herokuapp.com/place
13 web service integration
13 web service integration
13 web service integration
13 web service integration
Upcoming SlideShare
Loading in …5
×

13 web service integration

132 views

Published on

web service integration, REST API, retrofit

Published in: Travel
  • Be the first to comment

  • Be the first to like this

13 web service integration

  1. 1. Web Service Integration Anuchit Chalothorn anoochit@gmail.com
  2. 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. 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. 4. JSON JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write.
  5. 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. 6. Retrofit API Client retrofit= new Retrofit.Builder() .baseUrl("http://api.example.com/") .addConverterFactory(GsonConverterFactory.create()) .build();
  7. 7. Retrofit API Interface public interface ApiInterface { @GET("place") Call<List<Person>> doGetPlaceList(); } Note: request url → http://api.example.com/place
  8. 8. // 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
  9. 9. Picasso Picasso.with(context) .load(image_url) .error(R.drawable.placeholder_error) .placeholder(R.drawable.placeholder) .into(imageview);
  10. 10. Build.gradle compile 'com.google.code.gson:gson:2.8.0' compile 'com.squareup.retrofit2:retrofit:2.2.0' compile 'com.squareup.retrofit2:converter-gson:2.2.0' compile 'com.squareup.picasso:picasso:2.5.2'
  11. 11. Tools ● Advance REST Client ● JSONView ● jsonlint.com ● jsonschema2pojo.org
  12. 12. Codelab: Get data from JSON url JSON url https://raw.githubusercontent.com/anoochit/j sondata/master/data02.json
  13. 13. Codelab: Get data from REST API REST API https://warm-ridge-87805.herokuapp.com/place

×