Android Retrofit - PRABEESH R K
Some information we should know
{} → Means JSON object.
when we will try to fetch data from Any JSON or from any server then we should check very carefully that Scripts and should understand the type of JSON.
is that starts with JSON Array? OR JSON Object?
flowers.json [ start with array / list of objects ]
Vs
yahoo weather api [ start with the object named query which includes many objects. ]
🔑Add internet permission in manifest file.
🔑Add some Dependencies :
compile 'com.squareup.retrofit2:retrofit:2.2.0' compile 'com.squareup.picasso:picasso:2.5.2' compile 'com.squareup.retrofit2:converter-gson:2.2.0' ORcompile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.picasso:picasso:2.5.2' compile 'com.squareup.retrofit2:converter-gson:2.3.0'
So before going to create an Android application based on the retrofit you must have some theoretical concepts about the Retrofit.
So here I create a simple presentation that will give you the basic concepts of using Retrofit
in Android.
Introduction to Retrofit
🔘 Retrofit is a type safe rustic client for Android and Java developed by Square.
🔘 Retrofit uses okay HTTP library for HTTP requests.
[ retrofit it is developed on ok HTTP protocol ]
🔘 Retrofit is the best tool for performing network requests in android applications.
if you want to use a Retrofit in android you need 3 things first.
Retofit mainly need 3 things in Android.
🔑 Retrofit Instance.
🔑 Model Class or POJO Class.
🔑 An interface for possible API calls.
How to implements them ?
🔑 Retrofit Instance:
→You can create an instance of a Retrofit by Retrofit.builder class.
→You have to specify the base URL and converted Factory at the time of the
Retrofit instance creation.
here is a simple example :
String FUrl = "http://services.hanselandpetal.com/feeds/flowers.json";
//as we know base url never change so we putt it as final
private final String Base_Url = "http://services.hanselandpetal.com/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Base_Url)
.addConverterFactory(GsonConverterFactory.create())
.build();
/*baseURL never changed*/
[ note: so here we create an instance of a retrofit by using the Retrofit.builder class you have to specify the base URL through the baseURL method.you have to specify the converted factory through the addConvertedFactory method.here we use the GSON converted Factory and finally you have to call the builder method this will return an instance of Retrofit.]
🔑 Model Class or POJO Class.
→ Retrofit need a POJO class for sending and receiving requests.
→ retrofit uses the pojo class for parse the server responds by using converters like GSON,Jackson,Moshi etc.
here is an example for free JSon online :
And an example of Model class/ Pojo Class for that JSON:
[ note : You guys can use .jsonschema2pojo.org for generated JSON to POJO Class. ]
package com.nirjhor3029.retrofitbybitm;import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/** * Created by nirjhor on 2/14/2018. */
public class FlowerResponse {
@SerializedName("category")
@Expose private String category;
@SerializedName("price")
@Expose private Double price;
@SerializedName("instructions")
@Expose private String instructions;
@SerializedName("photo")
@Expose private String photo;
@SerializedName("name")
@Expose private String name;
@SerializedName("productId")
@Expose private Integer productId;
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getInstructions() {
return instructions;
}
public void setInstructions(String instructions) {
this.instructions = instructions;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
}
.[Note: here the annotation is serialized the category these annotations are used by GSON for serialization and deserialization]
🔑 An interface for possible API calls.
here
→ the interface contain methods that represents possible API calls.
→ each method need a base URL end point annotation that represents the HTTP methods like GET, POST etc.
→ the return type of each of these methods is an instance of call class.
here is a simple example :
package com.nirjhor3029.retrofitbybitm;
import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.http.GET;
/** * Created by nirjhor on 2/14/2018. */
public interface FlowerServiceAPI {
@GET("feeds/flowers.json") //End point of the URL
Call<ArrayList<FlowerResponse>> getFlowerResponse(); //Call class instance
//bujhi nai uporer bal da}
[ Note:
FlowerResponse is the POJO class we generated before.
here the interface name is FlowerServiceAPI that contains only one method called the getFlowerResponse() the return type of each of this method is an instance of call class.
here the target type is a Arraylist of object. The objects are FlowerResponse class objects and here each method need an annotation that
represents the HTTP methods like GET, POST etc so here we use the HTTP method
GET and in that annotation you have to specify endpoints of the base URL.
so here we use an endpoint called feeds/flowers.json.
]
Now Everything is set to go ... Let's make a request:
Make a Request:
now we can learn about how to make a
request through retrofit
.→ for sending a request we have to obtain an instance of interface by make a call to create method on retrofit instance → by using the interface instance you can make the needed API call through the interface methods.
here is an example:
/*baseURL never changed*/
flowerServiceAPI = retrofit.create(FlowerServiceAPI.class);
//Connection finish//Now we should retrieve dataCall<ArrayList<FlowerResponse>> arrayListCall = flowerServiceAPI.getFlowerResponse();
arrayListCall.enqueue(new Callback<ArrayList<FlowerResponse>>() {
@Override public void onResponse(Call<ArrayList<FlowerResponse>> call, Response<ArrayList<FlowerResponse>> response) {
//do what ever you want .
}
@Override public void onFailure(Call<ArrayList<FlowerResponse>> call, Throwable t) {
}
});
========== 😋😋😋😋😋😋😋😋 ======================
Now your Retrofit is ready and your Pojo Class is ready ... so what ever you want you can do ;)
[ retrofit it is developed on ok HTTP protocol ]
if you want to use a Retrofit in android you need 3 things first.
Retofit mainly need 3 things in Android.
🔑 Retrofit Instance.
🔑 Model Class or POJO Class.
🔑 An interface for possible API calls.
How to implements them ?
🔑 Retrofit Instance:
→You can create an instance of a Retrofit by Retrofit.builder class.
→You have to specify the base URL and converted Factory at the time of the
Retrofit instance creation.
here is a simple example :
[ note: so here we create an instance of a retrofit by using the Retrofit.builder class you have to specify the base URL through the baseURL method.you have to specify the converted factory through the addConvertedFactory method.here we use the GSON converted Factory and finally you have to call the builder method this will return an instance of Retrofit.]
→You can create an instance of a Retrofit by Retrofit.builder class.
→You have to specify the base URL and converted Factory at the time of the
Retrofit instance creation.
here is a simple example :
String FUrl = "http://services.hanselandpetal.com/feeds/flowers.json";
//as we know base url never change so we putt it as final
private final String Base_Url = "http://services.hanselandpetal.com/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Base_Url)
.addConverterFactory(GsonConverterFactory.create())
.build();
/*baseURL never changed*/
[ note: so here we create an instance of a retrofit by using the Retrofit.builder class you have to specify the base URL through the baseURL method.you have to specify the converted factory through the addConvertedFactory method.here we use the GSON converted Factory and finally you have to call the builder method this will return an instance of Retrofit.]
🔑 Model Class or POJO Class.
→ Retrofit need a POJO class for sending and receiving requests.
→ retrofit uses the pojo class for parse the server responds by using converters like GSON,Jackson,Moshi etc.
here is an example for free JSon online :
And an example of Model class/ Pojo Class for that JSON:
[ note : You guys can use .jsonschema2pojo.org for generated JSON to POJO Class. ]
.[Note: here the annotation is serialized the category these annotations are used by GSON for serialization and deserialization]
→ Retrofit need a POJO class for sending and receiving requests.
→ retrofit uses the pojo class for parse the server responds by using converters like GSON,Jackson,Moshi etc.
here is an example for free JSon online :
[ note : You guys can use .jsonschema2pojo.org for generated JSON to POJO Class. ]
package com.nirjhor3029.retrofitbybitm;import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/** * Created by nirjhor on 2/14/2018. */
public class FlowerResponse {
@SerializedName("category")
@Expose private String category;
@SerializedName("price")
@Expose private Double price;
@SerializedName("instructions")
@Expose private String instructions;
@SerializedName("photo")
@Expose private String photo;
@SerializedName("name")
@Expose private String name;
@SerializedName("productId")
@Expose private Integer productId;
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getInstructions() {
return instructions;
}
public void setInstructions(String instructions) {
this.instructions = instructions;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
}
.[Note: here the annotation is serialized the category these annotations are used by GSON for serialization and deserialization]
🔑 An interface for possible API calls.
here
→ the interface contain methods that represents possible API calls.
→ each method need a base URL end point annotation that represents the HTTP methods like GET, POST etc.
→ the return type of each of these methods is an instance of call class.
here is a simple example :
[ Note:
FlowerResponse is the POJO class we generated before.
here the interface name is FlowerServiceAPI that contains only one method called the getFlowerResponse() the return type of each of this method is an instance of call class.
here the target type is a Arraylist of object. The objects are FlowerResponse class objects and here each method need an annotation that
represents the HTTP methods like GET, POST etc so here we use the HTTP method
GET and in that annotation you have to specify endpoints of the base URL.
so here we use an endpoint called feeds/flowers.json.
]
Now Everything is set to go ... Let's make a request:
.→ for sending a request we have to obtain an instance of interface by make a call to create method on retrofit instance → by using the interface instance you can make the needed API call through the interface methods.
here is an example:
Now your Retrofit is ready and your Pojo Class is ready ... so what ever you want you can do ;)
here
→ the interface contain methods that represents possible API calls.
→ each method need a base URL end point annotation that represents the HTTP methods like GET, POST etc.
→ the return type of each of these methods is an instance of call class.
here is a simple example :
package com.nirjhor3029.retrofitbybitm;
import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.http.GET;
/** * Created by nirjhor on 2/14/2018. */
public interface FlowerServiceAPI {
@GET("feeds/flowers.json") //End point of the URL
Call<ArrayList<FlowerResponse>> getFlowerResponse(); //Call class instance
//bujhi nai uporer bal da}
[ Note:
FlowerResponse is the POJO class we generated before.
here the interface name is FlowerServiceAPI that contains only one method called the getFlowerResponse() the return type of each of this method is an instance of call class.
here the target type is a Arraylist of object. The objects are FlowerResponse class objects and here each method need an annotation that
represents the HTTP methods like GET, POST etc so here we use the HTTP method
GET and in that annotation you have to specify endpoints of the base URL.
so here we use an endpoint called feeds/flowers.json.
]
Now Everything is set to go ... Let's make a request:
Make a Request:
now we can learn about how to make a
request through retrofit
here is an example:
/*baseURL never changed*/
flowerServiceAPI = retrofit.create(FlowerServiceAPI.class);
//Connection finish//Now we should retrieve dataCall<ArrayList<FlowerResponse>> arrayListCall = flowerServiceAPI.getFlowerResponse();
arrayListCall.enqueue(new Callback<ArrayList<FlowerResponse>>() {
@Override public void onResponse(Call<ArrayList<FlowerResponse>> call, Response<ArrayList<FlowerResponse>> response) {
//do what ever you want .
}
@Override public void onFailure(Call<ArrayList<FlowerResponse>> call, Throwable t) {
}
});
========== 😋😋😋😋😋😋😋😋 ======================
Now your Retrofit is ready and your Pojo Class is ready ... so what ever you want you can do ;)
মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন