private boolean parseJson(String json) {
    boolean flag = false;
    if (!TextUtils.isEmpty(json)) {
      Log.i("discovery", "discovery  json!=null");
      JSONObject obj = FjsonUtil.parseObject(json);
      if (null == obj) {
        return false;
      }
      if (200 == obj.getIntValue("code")) {
        Log.i("discovery", "discovery  code==200");
        Page++;
        List<DiscoveryItemBean> mlist =
            FjsonUtil.parseArray(obj.getString("data"), DiscoveryItemBean.class);
        if (null == mlist) {
          return false;
        } else {
          for (int i = 0; i < mlist.size(); i++) {
            if (mlist.get(i).getNews() == null || mlist.get(i).getNews().size() < 1) {
              DiscoveryItemBean bean = mlist.get(i);
              mlist.remove(bean);
              i--;
            }
          }
          newAdapter.appendData(mlist, isClearOld);
          isClearOld = false;
          return true;
        }

      } else {
        Page--;
      }
    }
    return flag;
  }
  private void getDiscoveryServer() {
    Log.i("discovery", "discovery  getDiscoveryServer");
    if (Page == 1) {
      Log.i("discovery", "discovery  Page==1");
      String json = SharePreferecesUtils.getParam(getActivity(), DISCOVERY_CATEGERY, "").toString();
      if (!TextUtils.isEmpty(json)) {
        parseJson(json);
        isClearOld = true;
      } else {
        data_empty.setVisibility(View.VISIBLE);
        app_progress_bar.setVisibility(View.GONE);
        isFirst = true;
      }
    }
    Map<String, String> params = RequestParamsUtils.getMaps();
    params.put("Page", Page + "");
    params.put("PageSize", pageSize + "");
    SPUtil.addParams(params);
    OkHttpClientManager.postAsyn(
        tag,
        InterfaceJsonfile.discovery_url,
        new OkHttpClientManager.ResultCallback() {
          @Override
          public void onSuccess(Object response) {
            mSwipeRefreshWidget.setRefreshing(false);
            isFirst = false;
            data_empty.setVisibility(View.GONE);
            app_progress_bar.setVisibility(View.GONE);
            try {
              if (Page == 1) {
                if (isAdded()) {
                  SharePreferecesUtils.setParam(
                      getActivity(), DISCOVERY_CATEGERY, response.toString());
                }
              }
              boolean flag = parseJson(response.toString());
              if (!flag) {
                newAdapter.setShowLoading(false);
              }

            } catch (Exception e) {
              e.printStackTrace();
            }
          }

          @Override
          public void onFailure(Request request, Exception e) {
            mSwipeRefreshWidget.setRefreshing(false);
            if (isFirst) {
              data_empty.setVisibility(View.VISIBLE);
              app_progress_bar.setVisibility(View.GONE);
            }
            newAdapter.setShowLoading(false);
          }
        },
        params);
  }
  @Override
  public void onClick(View v) {
    try {
      if (v.getId() == R.id.click_refresh_btn) {
        app_progress_bar.setVisibility(View.VISIBLE);
        data_empty.setVisibility(View.GONE);
        getDiscoveryServer();
      }
      if (v.getTag() != null && v.getTag() instanceof DiscoveryItemNewAdapter.ItemViewHolder) {
        Log.i("DiscoveryItemNewAdapter.ItemViewHolder", "DiscoveryItemNewAdapter.ItemViewHolder");
        DiscoveryItemNewAdapter.ItemViewHolder viewHolder =
            (DiscoveryItemNewAdapter.ItemViewHolder) v.getTag();
        {
          Log.i(
              "DiscoveryItemNewAdapter",
              "DiscoveryItemNewAdapter  viewHolder.tv_subscribe  onClick");
          viewHolder.tv_subscribe.setBackgroundResource(R.drawable.corners_bg);
          viewHolder.tv_subscribe.setTextColor(
              getActivity().getResources().getColor(R.color.details_tv_check_color));
          Drawable nav_up =
              getActivity().getResources().getDrawable(R.drawable.discovery_image_select);
          nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
          viewHolder.tv_subscribe.setCompoundDrawables(nav_up, null, null, null);
          viewHolder.tv_subscribe.setText(getActivity().getString(R.string.discovery_followed));
          EventBus.getDefault().post(new TagEvent(viewHolder.tagBean));
          if (Utils.isNetworkConnected(getActivity())) {
            Map<String, String> params = RequestParamsUtils.getMapWithU();
            if (spu.getUser() != null) {
              params.put("uid", spu.getUser().getUid() + "");
            }
            params.put("tagId", viewHolder.tagBean.getId() + "");
            SPUtil.addParams(params);

            OkHttpClientManager.postAsyn(
                tag,
                InterfaceJsonfile.tag_click_url,
                new OkHttpClientManager.ResultCallback() {
                  @Override
                  public void onSuccess(Object response) {
                    Log.i("onSuccess", "onSuccess");
                  }

                  @Override
                  public void onFailure(Request request, Exception e) {
                    Log.i("onFailure", "onFailure");
                  }
                },
                params);
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Beispiel #4
0
 public void loadKeys(List<String> keys) {
   adapter.setData(keys);
   if (keys == null) {
     final File target =
         App.getFile(App.getInstance().getJsonFileCacheRootDir() + File.separator + "saerch_keys");
     Log.i("target.getAbsolutePath()", "target.getAbsolutePath()" + target.getAbsolutePath());
     try {
       String json = App.getFileContext(target);
       Log.e("test", json);
       org.json.JSONArray jsonArray = new JSONObject(json).getJSONArray("data");
       int lenght = jsonArray.length();
       keys = new ArrayList<String>();
       for (int i = 0; i < lenght; i++) {
         keys.add(jsonArray.getString(i));
       }
       adapter.setData(keys);
     } catch (Exception e) {
     }
   }
 }