Ejemplo n.º 1
0
 private void requestNewFeedRefresh() {
   StringRequest req =
       new StringRequest(
           Urls.SERVER_PATH + "/feed/unread_num?offset=" + offset,
           new Response.Listener<String>() {
             @Override
             public void onResponse(String response) {
               if (!TextUtils.isEmpty(response)) {
                 try {
                   JSONObject json = new JSONObject(response);
                   int count = json.getInt("unread_num");
                   String msg = json.getString("message");
                   if (count > 0 && !TextUtils.isEmpty(msg)) {
                     showRefreshNewsCount(msg, true);
                     stopRefreshNews();
                     return;
                   }
                 } catch (Exception e) {
                   e.printStackTrace();
                 }
                 startRefreshNews();
               }
             }
           },
           new Response.ErrorListener() {
             @Override
             public void onErrorResponse(VolleyError error) {
               Trace.d(TAG, error.getMessage());
               startRefreshNews();
             }
           });
   if (TextUtils.isEmpty(REFRESH_TAG)) REFRESH_TAG = getUniqueRequestTag();
   req.setHeaders(AppUtils.getOAuthMap(getActivity()));
   req.addHeader("lang", getString(R.string.lang));
   req.setShouldCache(false);
   ApplicationController.getInstance().addToRequestQueue(req, REFRESH_TAG);
 }
Ejemplo n.º 2
0
  /** @param type 0 refresh 1 more */
  private void request(String url, final int type) {
    if (type == 0) {
      stopRefreshNews();
      showRefreshNewsCount(null, false);
      refreshing = true;
      mHandler.removeCallbacks(stopRefreshRunnable);
    }
    if (adapter.getItemCount() == 0) {
      mEmptyView.show(true);
      mEmptyView.onLoading();
    }
    mEmptyView.setOnClickListener(null);
    GsonRequest<FeedDatasGsonModel> req =
        new GsonRequest<FeedDatasGsonModel>(
            url,
            FeedDatasGsonModel.class,
            AppUtils.getOAuthMap(getActivity()),
            new Response.Listener<FeedDatasGsonModel>() {
              @Override
              public void onResponse(FeedDatasGsonModel response) {
                if (response != null) {
                  nextUrl = response.next;
                  if (response.data != null && !response.data.isEmpty()) {
                    if (type == 0) {
                      data.clear();
                    }
                    data.addAll(response.data);
                  }
                }

                if (!TextUtils.isEmpty(nextUrl)) {
                  adapter.setLoadMoreEnable(true);
                  adapter.setLoadMoreState(0);
                } else {
                  adapter.setLoadMoreEnable(true);
                  adapter.setLoadMoreState(XListViewFooter.STATE_NOTDATA);
                }

                adapter.notifyDataSetChanged();
                if (type == 0) {
                  offset = response != null ? response.offset : null;
                  if (!TextUtils.isEmpty(offset)) {
                    try {
                      offset = URLEncoder.encode(offset, "UTF-8");
                    } catch (UnsupportedEncodingException e) {
                      e.printStackTrace();
                    }
                    startRefreshNews();
                  }
                  mRefresh.clearAnimation();
                  refreshing = false;
                  swipeRefreshLayout.setRefreshing(false);
                } else isLoading = false;
                if (adapter.getItemCount() <= 0) {
                  mEmptyView.show(true);
                  mEmptyView.onFailed(getString(R.string.no_data));
                } else if (mEmptyView.isShowing()) mEmptyView.show(false);
              }
            },
            new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                if (mRefresh != null) mRefresh.clearAnimation();
                if (type == 0) {
                  if (!TextUtils.isEmpty(offset)) {
                    startRefreshNews();
                  }
                  if (data.isEmpty()) {
                    mEmptyView.show(true);
                    ErrorEntity entity = AppUtils.getErrorEntity(error);
                    mEmptyView.onFailed(
                        entity != null && !TextUtils.isEmpty(entity.getMessage())
                            ? entity.getMessage()
                            : getString(R.string.chat_load_failed));
                    mEmptyView.setOnClickListener(mRefreshNewsListener);
                  }
                  refreshing = false;
                  swipeRefreshLayout.setRefreshing(false);
                } else {
                  adapter.setLoadMoreEnable(true);
                  adapter.setLoadMoreState(0);
                  adapter.notifyDataSetChanged();
                  isLoading = false;
                }
              }
            });
    req.setShouldCache(false);
    addRequest(req);
  }