private void getNoHanlderNumber() { OkHttpUtils.post() .url(接口) .build().execute(new StringCallback() { @Override public void onError(Call call, Exception e, int id) { showToast("获取失败,请检查网络"); } @Override public void onResponse(String response, int id) { if (!TextUtils.isEmpty(response)) { try { JSONObject jsonObject = new JSONObject(response); Iterator<String> keys = jsonObject.keys(); //迭代器 while (keys.hasNext()) { //判断是否还有下一个元素 String next = keys.next(); //如果有,便获取这个元素 Object count = jsonObject.get(next); int index = getIndexByStatus(next); if (index != -1) { View customView = bushead.getTabAt(index).getCustomView(); TextView tvUnHandlerCount = customView.findViewById(R.id.tv_handlerNumber); tvUnHandlerCount.setText(count + ""); if ((int) count > 0) { //逻辑判断 } } else { tvUnHandlerCount.setVisibility(View.GONE); } } } } catch (JSONException e) { e.printStackTrace(); } } } }); }
private int getIndexByStatus(String next) {
switch (next) {
case "0":
return 0;
case "1":
return 1;
case "3":
return 2;
case "4":
return 3;
case "5":
return 4;
case "7":
return 5;
case "10":
return 6;
case "11":
return 7;
}
return -1;
}
通过接口获得的返回数据
{ "0": 1, "1": 0, "2": 0, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0, "10": 0, "11": 0, "-1": 0 }