Hi i have trying to get the data from Magento using Rest API with the method of Retrofit but i referred many sites but i cant get any hope please help me to write correct coding i ll be helpfull..
My JSON format:
[{"id":2153,"sku":"62928","name":"3 ROSES DUST 250G TEA REFILL","attribute_set_id":4,"price":128,"status":1,"visibility":4,"type_id":"simple","created_at":"2017-08-15 13:21:32","updated_at":"2017-09-14 06:53:31","extension_attributes":{"stock_item":{"item_id":4150,"product_id":2153,"stock_id":1,"qty":45,"is_in_stock":true,"is_qty_decimal":false,"show_default_notification_message":false,"use_config_min_qty":true,"min_qty":0,"use_config_min_sale_qty":1,"min_sale_qty":1,"use_config_max_sale_qty":true,"max_sale_qty":10000,"use_config_backorders":true,"backorders":0,"use_config_notify_stock_qty":true,"notify_stock_qty":1,"use_config_qty_increments":true,"qty_increments":0,"use_config_enable_qty_inc":true,"enable_qty_increments":false,"use_config_manage_stock":true,"manage_stock":true,"low_stock_date":null,"is_decimal_divided":false,"stock_status_changed_auto":0}},"product_links":[],"options":[],"media_gallery_entries":[{"id":171,"media_type":"image","label":"","position":1,"disabled":false,"types":["image","small_image","thumbnail","swatch_image"],"file":"\/6\/2\/62928.png"}],"tier_prices":[],"custom_attributes":[{"attribute_code":"description","value":"
3 ROSES DUST 250G TEA REFILL<\/p>"},{"attribute_code":"short_description","value":"
3 ROSES DUST 250G TEA REFILL<\/p>"},{"attribute_code":"special_price","value":"121.6000"},{"attribute_code":"special_from_date","value":"2017-08-17 00:00:00"},{"attribute_code":"special_to_date","value":"2018-08-31 00:00:00"},{"attribute_code":"cost","value":"128.0000"},{"attribute_code":"meta_title","value":"3 ROSES DUST 250G TEA REFILL"},{"attribute_code":"meta_description","value":"3 ROSES DUST 250G TEA REFILL"},{"attribute_code":"image","value":"\/6\/2\/62928.png"},{"attribute_code":"small_image","value":"\/6\/2\/62928.png"},{"attribute_code":"thumbnail","value":"\/6\/2\/62928.png"},{"attribute_code":"news_from_date","value":"2017-08-17 11:51:23"},{"attribute_code":"custom_design_from","value":"2017-08-17 11:51:23"},{"attribute_code":"category_ids","value":["2","58","132"]},{"attribute_code":"options_container","value":"container2"},{"attribute_code":"required_options","value":"0"},{"attribute_code":"has_options","value":"0"},{"attribute_code":"msrp_display_actual_price_type","value":"0"},{"attribute_code":"url_key","value":"3-roses-dust-250g-tea-refill"},{"attribute_code":"gift_message_available","value":"2"},{"attribute_code":"tax_class_id","value":"2"},{"attribute_code":"swatch_image","value":"\/6\/2\/62928.png"}]}]
My needed Attributes are : name, sku, price, extension_attributes:"media_gallery":"file", attribute_code:"special_price",value:"121.6000"
I have set some coding as per my knowledge as follows:
Product:
public class Product {
@SerializedName("sku")
@Expose
private String sku;
@SerializedName("name")
@Expose
private String name;
@SerializedName("price")
@Expose
private String price;
@SerializedName("extension_attributes")
@Expose
private ExtensionAttributes extensionAttributes;
public String getSku() {
return sku;
}
public void setSku(String sku) {
this.sku = sku;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public ExtensionAttributes getExtensionAttributes() {
return extensionAttributes;
}
public void setExtensionAttributes(ExtensionAttributes extensionAttributes) {
this.extensionAttributes = extensionAttributes;
}
@Override
public String toString() {
return "Product{" +
"sku='" + sku + '\'' +
", name='" + name + '\'' +
", price='" + price + '\'' +
", extensionAttributes=" + extensionAttributes +
'}';
}
}
Extension attribute:
public class ExtensionAttributes {
@SerializedName("media_gallery_entries")
@Expose
private ArrayList<Media_Gallery> media;
public ArrayList<Media_Gallery> getMedia() {
return media;
}
public void setMedia(ArrayList<Media_Gallery> media) {
this.media = media;
}
}
Media_Gallery:
public class Media_Gallery {
@SerializedName("file")
@Expose
private String file;
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
}
Please check this above coding is correct.. But i have doubt in mainactivity that is how i should set this all above values in my recycler view please help me current mainactivity coding is below:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
List<Product> listing;
List<Media_Gallery> listing1;
private final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycle_retrofit);
recyclerView = (RecyclerView) findViewById(R.id.recycle);
listing = new ArrayList<>();
listing1 = new ArrayList<Media_Gallery>();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://alagendransupermart.com/mageapi/")
.addConverterFactory(GsonConverterFactory.create())
.build();
APiService service = retrofit.create(APiService.class);
Call<List<Product>> call= service.getbookdetails();
call.enqueue(new Callback<List<Product>>() {
@Override
public void onResponse(Call<List<Product>> call, Response<List<Product>> response) {
List<Product> list = response.body();
Product product = null;
String image=null;
for (int i =0 ;i<list.size();i++){
product = new Product();
String name = list.get(i).getName();
String color = list.get(i).getSku();
String price = list.get(i).getPrice();
ArrayList<Media_Gallery> media = response.body().get(i).getExtensionAttributes().getMedia();
for(int j = 0; j<media.size(); j++){
image = media.get(j).getFile();
}
Media_Gallery med = new Media_Gallery();
med.setFile(image);
product.setPrice(price);
product.setSku(color);
product.setName(name);
listing.add(product);
listing1.add(med);
}
Recycleradapter recyclerAdapter = new Recycleradapter(MainActivity.this,listing);
RecyclerView.LayoutManager recyce = new GridLayoutManager(MainActivity.this,2);
// RecyclerView.LayoutManager recyce = new LinearLayoutManager(MainActivity.this);
recyclerView.addItemDecoration(new GridSpacingdecoration(2, dpToPx(10), true));
recyclerView.setLayoutManager(recyce);
recyclerView.setItemAnimator( new DefaultItemAnimator());
recyclerView.setAdapter(recyclerAdapter);
}
@Override
public void onFailure(Call<List<Product>> call, Throwable t) {
Log.d(TAG,"Something went wrong :"+t.getMessage());
}
});
}
}