首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android使用HttpClient和Apache服务器运行php脚本和Memcache的性能

Android使用HttpClient和Apache服务器运行php脚本和Memcache的性能
EN

Stack Overflow用户
提问于 2014-04-28 10:48:02
回答 1查看 183关注 0票数 0

我有一个应用程序,其中Android设备连接到数据采集系统,数据存储在本地数据库中。我也想在第二个Android上实时监控数据。为了增强性能,我在Windows机器上设置了我自己的Apache服务器(XAMPP)。我编写了两个php脚本;一个用于在memcache中存储数据,另一个用于从Memcache中获取数据。安卓设备正在使用HttpClient来调用脚本。我使用memcache是为了消除写入数据库的一些延迟。下面是用于发送和接收数据的代码...为了进行测试,它是一个带有线程延迟的简单循环(计数器)。如果我将延迟设置为1秒,文本视图将更新并顺利运行。当我将延迟降低到0.1秒时,它会正常运行几秒钟,然后卡顿(暂停) 3-5秒。它在运行时继续这个间歇性暂停的过程。如果我将延迟降低到0.01秒,系统就会崩溃。我的Apache服务器直接连接到我的路由器。安卓设备已连接到我的WiFi。我的问题是,我是否做了一些根本错误的事情。我认为使用memcache,我可以轻松地完成0.01s的数据采样。我的代码是否正确地处理了AsyncTasks?我怎样才能防止Android系统被新的任务“超载”呢?这就好像我需要一个方法来确定任务何时完成,以便可以调用下一个任务。我很感谢任何关于php,httpclient,etc...anything的影响性能的反馈。

编辑:当我将延迟降低到0.01秒时,我的系统不再崩溃。我现在有了对onpostexecute方法中空值的捕获。但是,我仍然会卡顿/冻结。

代码语言:javascript
复制
    private void startsendingdata() {
            loop = 0;
            if (sendingdatathread != null)
                sendingdatathread.interrupt();
            sendingdatathread = new Thread() {
                public void run() {
                    while (loop < 5000) {
                        try {
                            runOnUiThread(new Runnable() {

                                @Ov

erride
                            public void run() {
                                new SendDataTask().execute();
                                loop++;
                                myvar = Integer.toString(loop);
                            }
                        });
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            }

        };
        sendingdatathread.start();

    }

    private class SendDataTask extends AsyncTask<String, Integer, Double> {

        @Override
        protected Double doInBackground(String... params) {
            postData();
            return null;
        }

        protected void onPostExecute(Double result) {
        }

        public void postData() {
            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                // put all variables here (with connection code attached)
                nameValuePairs.add(new BasicNameValuePair("connectioncode",
                        connectioncode));

                nameValuePairs.add(new BasicNameValuePair("myvar", myvar));

                httppostsend
                        .setEntity(new UrlEncodedFormEntity(nameValuePairs));
                response = httpclient.execute(httppostsend);
                response.getEntity().consumeContent();

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

    private void startgettingdata() {

        if (gettingdatathread != null)
            gettingdatathread.interrupt();
        gettingdatathread = new Thread() {
            public void run() {
                while (true) {
                    try {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                new GetDataTask().execute();
                            }
                        });
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        break;
                    }
                }
            }

        };
        gettingdatathread.start();
    }

    private class GetDataTask extends AsyncTask<String, Integer, Double> {

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

            getData();

            return null;
        }

        protected void onPostExecute(Double result) {
            if (!line.isEmpty() && line!=null&&connectioncode.length() >= 6) {


                tvLapTime.setText("MyVar: " + myvar);
            }

        }

        public void getData() {
            if (connectioncode.length() >= 6) {
                try {
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("connectioncode",
                            connectioncode));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    response = httpclient.execute(httppost);
                    entity = response.getEntity();
                    is = entity.getContent();
                    line = convertInputStreamToString(is);
                    response.getEntity().consumeContent();
                } catch (ClientProtocolException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    // convert inputstream to String
        private static String convertInputStreamToString(InputStream inputStream)
                throws IOException {
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(inputStream));
            String result = "";
            while ((line = bufferedReader.readLine()) != null)
                result += line;
        try {
            json = new JSONObject(result);
        } catch (JSONException e1) {
            e1.printStackTrace();
        }

        if (json != null) {
            try {

                myvar = json.getString(connectioncode + "myvar");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        inputStream.close();
        return result;
    }
EN

回答 1

Stack Overflow用户

发布于 2014-04-28 15:35:29

去掉startsendingdata()和startgettingdata()。在任务的onPostExecute()中,启动下一个任务的开始。现在,在开始新的任务之前,您可以确定任务已完成。例如,你可以启动一个定时器来执行下一个任务。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23331742

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档