快速将数据从mysql加载到recyclerview

时间:2017-01-21 15:14:51

标签: php android mysql database android-recyclerview

我有一个Android应用程序,它将数据从php加载到recyclerview并在卡片上显示,每次滚动器到达某个点时我加载更多数据,问题是它仍然加载数据非常缓慢而且我不知道如何让它加载更快。

这是加载数据的BackgroundTask

 public class BackgroundTask extends AsyncTask<Void, Void, String> {
        String json_url;


        protected void onPreExecute() {
            json_url = "http://website.com/file.php";
        }

        @Override
        protected String doInBackground(Void... params) {

            try {
                String urlLimit = URLEncoder.encode("limit", "UTF-8") + "=" + URLEncoder.encode(limit+"", "UTF-8") + "&" +
                        URLEncoder.encode("offset", "UTF-8") + "=" + URLEncoder.encode(offest+"", "UTF-8");

                String   data = urlLimit ;


                URL url = new URL(json_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                // if(AgeOn == true) {
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                bufferedWriter.write(data);
                bufferedWriter.flush();
                //    }
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder stringBuilder = new StringBuilder();


                while ((JSON_STRING = bufferedReader.readLine()) != null) {

                    stringBuilder.append(JSON_STRING + "\n");

                }

                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return stringBuilder.toString().trim();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return "";
        }





        @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
            Log.d("results:", result + "");
            items = result.split(",:,");
            id = new int[items.length / 10];
            name = new String[items.length / 10];
            lastname = new String[items.length / 10];
            date = new String[items.length / 10];


            List<ListItem> data = new ArrayList<>();

            int finish = 0;
            int counter = 0;

            while (finish < items.length) {
                id[counter] = Integer.parseInt(items[finish]);
                name[counter] = items[finish + 1];
                lastname[counter] = items[finish + 2];
                date[counter] = items[finish + 3];

                finish = finish + 4;
                counter++;

            }
        }
            SetViews();

    }

}

加载后,只需调用一个方法来创建回收站视图的项目并设置列表。

这是加载数据的php文件

  <?php
header('Content-Type: text/html; charset=utf-8');
    $db = new PDO('mysql:host**********=;dbname=*******;charset=utf8mb4',******** , '********');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);


$limit =$_POST["limit"];
$offset =$_POST["offset"];



$stmt = $db->query("SELECT * FROM appdb LIMIT $limit OFFSET $offset");

$results = $stmt->fetchAll(PDO::FETCH_ASSOC);


$out = '';
if (is_array($results) && count($results) > 0) 
{
  foreach ($results as $sqlRow) 
  {
    $out .= $sqlRow['id'].",:,".$sqlRow['name'].",:,".$sqlRow['lastname'].",:,".$sqlRow['date'].",:,";
  }

  $out = substr($out, 0, -3);
}
echo $out;


?>      

如何更快地加载数据?

0 个答案:

没有答案