URL: http://btownmedia.com/wp-json/wp/v2/categories/?parent=10
step 1:
public interface RetrofitArrayAPI {
@GET("wp/v2/categories/") Call<List<ChildCategories>> getListCat(@Query("parent")int pid);}
step 2:
out side the onCreate Method
private void getCategoriesList(int pid) { Retrofit retrofit = new Retrofit.Builder() .baseUrl(url) .addConverterFactory(GsonConverterFactory.create()) .build(); RetrofitArrayAPI service = retrofit.create(RetrofitArrayAPI.class); Call<List<ChildCategories>> call = service.getListCat(pid); call.enqueue(new Callback<List<ChildCategories>>() { @Override public void onResponse(Response<List<ChildCategories>> response, Retrofit retrofit) { try { List<ChildCategories> CategoryData = response.body(); for (int i = 0; i < CategoryData.size(); i++) { ChildCategories b = new ChildCategories(); b.setId(CategoryData.get(i).getId()); b.setName(CategoryData.get(i).getName()); // Log.d("Category[" + i + "]", String.valueOf(CategoryData.get(i).getId())); // Log.d("CategoryName", String.valueOf(CategoryData.get(i).getName())); //Log.d(TAG, String.valueOf(CategoryData.get(i).getName())); bsArry.add(b); } ChildCategories cc = new ChildCategories(); for (int i = 0; i < bsArry.size(); i++) { cc = (ChildCategories) bsArry.get(i); Log.d("Business", cc.getName()); bsinfi.add(cc.getName()); } } catch (Exception e) { Log.d("onResponse", "There is an error"); e.printStackTrace(); } } @Override public void onFailure(Throwable t) { Log.d("onFailure", t.toString()); } }); }
Step 3:
call in onCreate() method {
getCategoriesList(7)
}
Comments
Post a Comment