【问题标题】:Fetch JSON Data from URL and Repeatedly Update SQLite Database从 URL 中获取 JSON 数据并重复更新 SQLite 数据库
【发布时间】:2014-06-27 14:28:24
【问题描述】:

在我的应用程序中,我创建了一个 SQLite 数据库。然后,我使用主要活动中的 HttpAsyncTask 类的实例从 URL 获取的 JSON 数据填充它。这很好,但我也想更新数据库。每天一次将新数据(数据库中的一行)添加到 URL 页面,我想在应用程序中实现一个“同步”按钮,该按钮仅使用新信息更新数据库。我能得到一些关于如何做到这一点的建议吗?我的 HttpAsyncTask 在下面,如果有帮助的话 - 我想我可能需要 onPostExecute() 方法中的 if/else 子句,仅在第一次创建数据库时才添加所有行。我想过尝试在我的 DatabaseHelper 中放置一个 HttpAsyncTask 类,但这并没有什么意义。

private class HttpAsyncTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String...urls) {
        return GET(urls[0]);
    }

    @Override
    protected void onPostExecute(String result) {

        try {
            JSONObject main = new JSONObject(result);
            JSONObject body = main.getJSONObject("body");
            JSONArray measuregrps = body.getJSONArray("measuregrps");

            // get measurements for date, unit, and value (weight)
            for (int i = 0; i < measuregrps.length(); i++) {
               JSONObject row = measuregrps.getJSONObject(i);
               // a lot of getting & parsing data happens
               db.addEntry(new Entry(date, weight, null, null)); 
               //adds all the lines every time this is run, but I only want to add all 
               //the lines once and then add new rows one by one from there
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

public static String GET(String url) {
    InputStream is = null;
    String result = "";
    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(url);
        HttpResponse httpResponse = client.execute(get);
        is = httpResponse.getEntity().getContent();
        if (is != null)
            result = convertInputStream(is);
        else
            result = "Did not work!";
    } catch (Exception e) {
        Log.d("input stream", e.getLocalizedMessage());
    }

    return result;
}

private static String convertInputStream(InputStream is) throws IOException {
    StringBuilder builder = new StringBuilder();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    String line = "";
    while((line = reader.readLine()) != null)
        builder.append(line);
    is.close();
    return builder.toString();
}

【问题讨论】:

    标签: java android json sqlite http


    【解决方案1】:

    您的实施完全取决于项目要求。 如果服务器不断变化,实现同步过程的正确方法是:

    1。 实现完全在后台运行的同步过程。将自定义此同步以调用同步所需的特定 API 调用/服务类。

    2。 服务器将提示移动客户端进行数据更改。

    3。 为了获得服务器更新,将运行以某些预定义的时间间隔连续运行的服务/同步并检查更新或实施 GCM。

    同步适配器最适合同步服务。 哦,别忘了应用内容提供者,因为数据库调用将同时从 UI 和后台进行。

    希望它可以帮助您做出决定。

    【讨论】:

    • 对不起,我不应该一直说。我只想在用户按下“同步”按钮时获取新数据并更新数据库。我觉得这应该很容易 - 如果网站的数据行数比我的数据库中的行数多两行,请添加两行最新数据。我只是不确定将代码放在哪里。
    • 在 Infra 下创建 Service 类,根据您的要求调用。
    【解决方案2】:

    如果是,则必须检查表中是否有类似的数据可用,如果是,则更新表,如果没有,则向表中插入新数据

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 2021-01-02
      • 2019-10-04
      • 2015-12-13
      • 2022-01-07
      • 1970-01-01
      • 2017-09-22
      • 1970-01-01
      • 2012-07-05
      相关资源
      最近更新 更多