Пример #1
0
  // 加载更多
  private void loadMore(final String url) {
    isLoading = true; // 正在加载
    if (HttpUtils.isNetworkConnected(mActivity)) {
      XUtil.GetJson(
          url,
          new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
              //                    PreUtils.putStringTo(Constant.CACHE, mActivity, url,
              // responseString);
              SQLiteDatabase db =
                  ((MainActivity) mActivity).getCacheDbHelper().getWritableDatabase();
              db.execSQL(
                  "replace into CacheList(date,json) values(" + date + ",' " + result + "')");
              db.close();
              parseBeforeJson(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
              //                    UIUtils.showToastSafe("解析失败: "+ex.toString());
            }
          });

    } else {
      SQLiteDatabase db = ((MainActivity) mActivity).getCacheDbHelper().getReadableDatabase();
      Cursor cursor = db.rawQuery("select * from CacheList where date = " + date, null);
      if (cursor.moveToFirst()) {
        String json = cursor.getString(cursor.getColumnIndex("json"));
        parseBeforeJson(json);
      } else {
        db.delete("CacheList", "date < " + date, null);
        isLoading = false;
        Snackbar sb = Snackbar.make(lv_news, "没有更多的离线内容了~", Snackbar.LENGTH_SHORT);
        sb.getView()
            .setBackgroundColor(
                mActivity
                    .getResources()
                    .getColor(
                        ((MainActivity) mActivity).isLight()
                            ? android.R.color.holo_blue_dark
                            : android.R.color.black));
        sb.show();
      }
      cursor.close();
      db.close();
    }
  }
Пример #2
0
  private void loadFirst() {
    isLoading = true;
    // 判断手机是否联网
    if (HttpUtils.isNetworkConnected(mActivity)) {
      XUtil.GetJson(
          Constant.BASEURL + Constant.LATESTNEWS,
          new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
              //                    UIUtils.showToastSafe("解析到的数据为: " + result);
              // 保存到本地数据库,做缓存
              SQLiteDatabase db =
                  ((MainActivity) mActivity).getCacheDbHelper().getWritableDatabase();
              // 更新本地数据
              db.execSQL(
                  "replace into CacheList(date,json) values("
                      + Constant.LATEST_COLUMN
                      + ",' "
                      + result
                      + "')");
              db.close(); // 关闭数据库
              // 解析数据
              parseLatestJson(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
              //                    UIUtils.showToastSafe("解析失败: "+ex.toString());
            }
          });
    }
    // 如果没有联网,从本地数据读取数据
    else {
      SQLiteDatabase db = ((MainActivity) mActivity).getCacheDbHelper().getReadableDatabase();
      Cursor cursor =
          db.rawQuery("select * from CacheList where date = " + Constant.LATEST_COLUMN, null);
      if (cursor.moveToFirst()) {
        String json = cursor.getString(cursor.getColumnIndex("json"));
        parseLatestJson(json);
      } else {
        isLoading = false;
      }
      cursor.close();
      db.close();
    }
  }