2

我有一个电子商务应用程序,当 5 到 10 个用户使用它时它可以完美运行。

但是当它被 50-60 人使用时,它变得非常慢。

目前我正在使用 MySQL 和 PHP。

我正在调用.php具有MySQL连接代码的文件。从那里我正在获取 JSON 响应。

下面是我的代码:

    class Items extends AsyncTask<String, String, String> {

    protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("id", Itemid));
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_allitems, "GET",
                    params);



            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {

                    products = json.getJSONArray(TAG_ITEMS);


                    for (int i = 0; i < products.length(); i++) {

                        JSONObject c = products.getJSONObject(i);

                        // Storing each json item in variable
                        String id = c.getString(TAG_ITEMID);
                        String name = c.getString(TAG_NAME);


                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_PID, id);
                        map.put(TAG_NAME, name);


                        // adding HashList to ArrayList
                        productsList.add(map);
}

这是我的 PHP 代码:

  $result = mysql_query("SELECT * FROM item_master") ;

// check for empty result
if (mysql_num_rows($result) > 0) {
    $response["items"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $item = array();
        $item["id"] = $row["id"];
        $item["code"] = $row["code"];
        $item["name"] = $row["name"];
        $item["description"] = $row["description"];
        $item["price"] = $row["price"];
        $item["image1"] = $row["image1"];

        // push single product into final response array
        array_push($response["items"], $product);

    }
    // success
    $response["psuccess"] = 1;
   ....
}

那么在这种情况下优化的最佳方法是什么?当超过 1000 名用户访问此应用时,该应用应具有响应能力并快速加载项目。

4

1 回答 1

2

在服务器端,您需要研究管理大量连接的技术,例如使服务器能够处理大量流量,以及动态负载平衡等网络级问题。

在客户端,您可以使用Volleywith okHttp。您还必须启用在okHttp服务器端的使用才能正常工作。

参考:

1. 在高负载站点中使用 PHP 的策略

2. 高流量网站上的动态和静态内容分离

3 、如何用OkHttp 2.0实现Android Volley?

4. 在您​​的 Web 服务器上使用 SPDY

于 2015-06-08T04:28:48.720 回答