【问题标题】:Android JSON API for wordpress taking too long to fetch data用于 wordpress 的 Android JSON API 需要很长时间才能获取数据
【发布时间】:2017-01-06 15:33:49
【问题描述】:

我一直在努力从 https://wordpress.org/plugins/json-api/other_notes/ 获取 wordpress 的 JSON API 数据

我已使用以下方法获取数据。一切正常,只是将数据加载到 tabview 大约需要 30 秒。

try {

            JSONObject jObj = new JSONObject(result);

            int count = (jObj.getInt("count_total"));


            JSONArray jArray = jObj.getJSONArray("posts");



            //  Toast.makeText(getApplicationContext(), "Year: " + date2.substring(0, 4) + "Hour: " + date2.substring(11, 13), Toast.LENGTH_SHORT).show();
            //  Toast.makeText(getApplicationContext(),"Hour: "+date2.substring(11,13),Toast.LENGTH_SHORT).show();
            // Extract data from json and store into ArrayList as class objects

            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);

                JSONArray jArray2 = json_data.getJSONArray("categories");
                JSONObject json_data2 = jArray2.getJSONObject(0);


                DataFish fishData = new DataFish();
                fishData.fishImage = json_data.getString("thumbnail");

                fishData.fishName = json_data.optString("title", "N/A");

                String comment = json_data.optString("comment_count", "0");
                fishData.comment = comment;
                 //fishData.fishName ="Title";

                //  fishData.sizeName = json_data.getString("size_name");
                String category_array = json_data2.optString("title", "N/A");


                fishData.sizeName = category_array;
                fishData.price = 10;
                fishData.url =
                        json_data.optString("url", "N/A");
                data.add(fishData);


            }
}

还有什么其他方法可以更快地获取数据,让用户无需等待可怕的 25-30 秒来加载数据?

【问题讨论】:

  • 你从 wp 收到了多少帖子?
  • @PabloCegarra 一次只有 5 个帖子
  • 而延迟时间是解析json还是请求数据?

标签: android json wordpress


【解决方案1】:

您应该使用新的和高度使用的库来进行 JSON 或 xml api 集成或数据转换库。

// retrofit, gson
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'

还可以查看此链接,它可以帮助您了解如何实现或使用它

http://www.androidhive.info/2016/05/android-working-with-retrofit-http-library/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    • 2011-07-09
    相关资源
    最近更新 更多