API HIT Shutter APP
MainActivity.Java -
package com.example.krishandev.shutterappapihitexample; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.example.krishandev.shutterappapihitexample.contracter.GetProductsByCategoryContracter; import com.example.krishandev.shutterappapihitexample.controller.GetProductsByCategoryController; import com.example.krishandev.shutterappapihitexample.model.GetProductsByCategoryResponseModel; import java.util.List; public class MainActivity extends AppCompatActivity implements GetProductsByCategoryContracter.GetProductsByCategoryView{ public String categoryid ="5b9b37c76bcd3b08803dfa29"; GetProductsByCategoryContracter.GetProductsByCategories getProductsByCategories; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getProductsByCategories = new GetProductsByCategoryController(this); getProductsByCategories.getProductsByCategoris(categoryid); } @Override public void showProgres() { } @Override public void hideProgrss() { } @Override public void handleGetProductsByCategory(List<GetProductsByCategoryResponseModel> getProductsByCategoryResponseModels) { } }
------------------------------------------------------------------
GetProductsByCategoryController.Java -package com.example.krishandev.shutterappapihitexample.controller; import android.content.Context; import android.util.Log; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import com.example.krishandev.shutterappapihitexample.contracter.GetProductsByCategoryContracter; import com.example.krishandev.shutterappapihitexample.model.GetProductsByCategoryResponseModel; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.List; public class GetProductsByCategoryController implements GetProductsByCategoryContracter.GetProductsByCategories{ GetProductsByCategoryContracter.GetProductsByCategoryView getProductsByCategoryView; Context context; public GetProductsByCategoryController(Context context){ this.context=context; getProductsByCategoryView= (GetProductsByCategoryContracter.GetProductsByCategoryView) context; } private RequestQueue requestQueue; public void getProductsByCategoris(String categoryid){ String url="http://18.188.63.119/users/getProductsByCategory?categoryid="+categoryid; requestQueue= Volley.newRequestQueue(context); StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { try { Log.i("seller", response); JSONObject jsonObject=new JSONObject(response); if (jsonObject.has("result")){ List<GetProductsByCategoryResponseModel> list=new ArrayList<>(); JSONArray jsonArray=jsonObject.getJSONArray("result"); for (int i=0; i<jsonArray.length(); i++) { JSONObject jsonObject1=jsonArray.getJSONObject(i); GetProductsByCategoryResponseModel getProductsByCategoryResponseModel=new GetProductsByCategoryResponseModel(); if (jsonObject1.has("_id")){ getProductsByCategoryResponseModel.setId(jsonObject1.getString("_id")); } if (jsonObject1.has("productname")){ getProductsByCategoryResponseModel.setProductname(jsonObject1.getString("productname")); } if (jsonObject1.has("description")){ getProductsByCategoryResponseModel.setDescription(jsonObject1.getString("description")); } if (jsonObject1.has("detail")){ getProductsByCategoryResponseModel.setDetails(jsonObject1.getString("detail")); } if (jsonObject1.has("sellingprice")){ getProductsByCategoryResponseModel.setSellingprice(jsonObject1.getString("sellingprice")); } if (jsonObject1.has("categoryid")){ getProductsByCategoryResponseModel.setId(jsonObject1.getString("categoryid")); } list.add(getProductsByCategoryResponseModel); } getProductsByCategoryView.handleGetProductsByCategory(list); } } catch (JSONException e){e.printStackTrace();}; } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); requestQueue.add(stringRequest); } }---------------------------------------------------------------GetProductsByCategoryContracter.java -package com.example.krishandev.shutterappapihitexample.contracter; import com.example.krishandev.shutterappapihitexample.model.GetProductsByCategoryResponseModel; import java.util.List; public interface GetProductsByCategoryContracter { interface GetProductsByCategoryView{ void showProgres(); void hideProgrss(); void handleGetProductsByCategory(List<GetProductsByCategoryResponseModel> getProductsByCategoryResponseModels); } interface GetProductsByCategories{ void getProductsByCategoris(String categoryid); } }------------------------------------------------------------GetProductsByCategoryResponseModel -
package com.example.krishandev.shutterappapihitexample.model; public class GetProductsByCategoryResponseModel { private String id; private String productname; private String description; private String details; private String sellingprice; private String categoryid; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getProductname() { return productname; } public void setProductname(String productname) { this.productname = productname; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getDetails() { return details; } public void setDetails(String details) { this.details = details; } public String getSellingprice() { return sellingprice; } public void setSellingprice(String sellingprice) { this.sellingprice = sellingprice; } public String getCategoryid() { return categoryid; } public void setCategoryid(String categoryid) { this.categoryid = categoryid; } }---------------------------------------------------------------------
Create 3 packages folders - 1. contractor 2. Model 3. controller1.In contractor package folder - We will create interfaces 1. For View All Products 2. For Get All Products.In “For View All Products” interface - create abstract methods. One Method type - array2.Define variables in Model class and create getter and setter methods.3.Create a Class and Implement “Get All Products” interface in itDefine context and View All Products interface.Create constructor - Pass context and assign context to View All Products interface.Define Request Queue.Implement “Get All Products” interface method.Write Hit API code under “Get All Products” interface method.Hit API -Define API URL.Make object of Request Queue.Make object of String Request.
Comments
Post a Comment