title: How to call more than one api in single activity by making separate class for all volley operations
body: <p>I have created a separate class in which I have defined all about volley and in another activity, I have directly pass URL, CONTEXT and Get Response...</p>
  
  <p>but in my <strong>NavDrawerActivity.java</strong> how do I call the  <strong>subCategoryJSON();</strong> method without writing my volley code again as I have done with <strong>mainCategoryJSON();</strong> method in which I just simply pass the URL, method type.</p>
  
  <p>Also is this a correct approach I am doing or there need to be some modification in the code, what I want is that wherever I am using API in my project and using volley for it, I don't have to write code again and again just simply pass the URL,method type </p>
  
  <p><strong>VolleyResponseListener.java</strong></p>
  
  <pre><code>public interface VolleyResponseListener {
  
      void onResponse(String response, String tag);
  
      void onError(VolleyError error, String tag);
  }
  </code></pre>
  
  <p><strong>CustomStringRequestVolley.java</strong></p>
  
  <pre><code>public class CustomStringRequestVolley {
  
      private String url;
      private String tag;
      Context ctx;
      private VolleyResponseListener volleyResponseListener;
  
  
      public CustomStringRequestVolley(String url, String tag,Context ctx,VolleyResponseListener volleyResponseListener){
          this.url = url;
          this.tag = tag;
          this.ctx=ctx;
          this.volleyResponseListener = volleyResponseListener;
          sendRequest();
      }
  
      private void sendRequest() {
          final ProgressDialog pDialog = new ProgressDialog(ctx);
          pDialog.setMessage("Loading ...");
          pDialog.show();
  
          StringRequest stringRequest = new StringRequest(Request.Method.GET,url,
                  new Response.Listener&lt;String&gt;() {
                      @Override
                      public void onResponse(String response) {
                          Log.e("catresponse", "response " + response);
                          if (pDialog.isShowing()) {
                              pDialog.dismiss();
                          }
                          volleyResponseListener.onResponse(response, tag);
                      }
                  },
                  new Response.ErrorListener() {
                      @Override
                      public void onErrorResponse(VolleyError error) {
                          error.printStackTrace();
                      }
                  });
  
  
            stringRequest.setRetryPolicy(new DefaultRetryPolicy(
                       5000,
                       DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                       DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
  
         VolleySingleton.getInstance(ctx).addToRequestQueue(stringRequest);
  
      }
  }
  </code></pre>
  
  <p><strong>NavDrawerActivity.java</strong></p>
  
  <pre><code>public class NavDrawerActivity extends AppCompatActivity
          implements NavigationView.OnNavigationItemSelectedListener, VolleyResponseListener {
  
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_nav_drawer);
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
        mainCategoryJSON();
         subCategoryJSON(); 
        }
  
  
    private void mainCategoryJSON() {
  
          CustomStringRequestVolley request1 = new CustomStringRequestVolley(URLs.categoryURL, "TAG1", this, this);
  
      }
  
      @Override
      public void onResponse(String response, String tag) {
  
          switch (tag) {
              case "TAG1":
                  try {
                      Log.i("Responseeeeeezaq :", response.toString() + "   " + tag);
  
  
                    JSONObject obj = new JSONObject(response);
  
                      JSONArray productArray = obj.getJSONArray("categories");
  
  
                      for (int i = 0; i &lt; productArray.length(); i++) {
  
                          JSONObject productObject = productArray.getJSONObject(i);
  
                          CategoryModelClass categoryModelClass = new CategoryModelClass();
                          categoryModelClass.setCategoryID(productObject.getInt("Category-Id"));
                          categoryModelClass.setCategoryName(productObject.getString("Category-Name"));
                          categoryModelClass.setCategoryImg(productObject.getString("Category-Image"));
                          categoryArrayList.add(categoryModelClass);
  
                          Log.d("zpuyi", String.valueOf(categoryArrayList));
  
                      }
  
                      categoryAdapter.notifyDataSetChanged();
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  break;
          }
  
      }
  
      @Override
      public void onError(VolleyError error, String tag) {
          VolleyLog.e("Error: ", error.getMessage());
      }
  
  
    private void subCategoryJSON() {
  
          StringRequest stringRequest = new StringRequest(Request.Method.GET, URLs.subcategoryURL,
                  new Response.Listener&lt;String&gt;() {
                      @Override
                      public void onResponse(String response) {
                          Log.d("subcategoryJsonResponse", response.toString());
  
                          try {
  
  
                              JSONObject obj = new JSONObject(response);
  
                              JSONArray productArray = obj.getJSONArray("sub-categories");
  
                              for (int i = 0; i &lt; productArray.length(); i++) {
  
                                  JSONObject productObject = productArray.getJSONObject(i);
  
                                  SubCategoryModelClass subCategoryModelClass = new SubCategoryModelClass();
                                  subCategoryModelClass.setSubCategory_Id(productObject.getInt("Subcategories-Id"));
                                  subCategoryModelClass.setCat_id(productObject.getInt("categories-Id"));
                                  subCategoryModelClass.setSubCategory_Name(productObject.getString("Subcategories-Name"));
                          subCategoryModelClass.setSubCategory_Img(productObject.getString("Subcategories-Image"));
                                  subCategoryModelClassList.add(subCategoryModelClass);
  
                                  Log.d("subCategoryArraylist", String.valueOf(subCategoryModelClassList));
  
                              }
  
                              for (int i = 0; i &lt; subCategoryModelClassList.size(); i++) {
                                  subcategory_id = subCategoryModelClassList.get(i).getSubCategory_Id();
                                  category_id = subCategoryModelClassList.get(i).getCat_id();
                                  subcategory_name = subCategoryModelClassList.get(i).getSubCategory_Name();
                               //   subcategory_desc = subCategoryModelClassList.get(i).getSubCategory_Desc();
                                  subcategory_image = subCategoryModelClassList.get(i).getSubCategory_Img();
                                  Log.d("fdsaff", subcategory_image);
  
                                  SQLiteDatabase database = dbHelper.getWritableDatabase();
                                  dbHelper.insertSubCategoryProduct(subcategory_id, category_id, subcategory_name, "https://www.ecrm.sample.in/app/img/"+subcategory_image, database);
                                  dbHelper.close();
                              }
  
                              subCategoryAdapter.notifyDataSetChanged();
  
  
                          } catch (JSONException e) {
                              e.printStackTrace();
                          }
                      }
                  },
                  new Response.ErrorListener() {
                      @Override
                      public void onErrorResponse(VolleyError error) {
                          VolleyLog.e("Error: ", error.getMessage());
                      }
                  });
  
          VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);
      }
  
  
  }
  </code></pre>