Android使用HttpClient的java性能,Apache服务器使用Memcache运行php脚本
我有一个应用程序,其中安卓设备连接到数据采集系统,数据存储在本地数据库中。我想在第二台Android上实时监控数据。为了提高性能,我在Windows机器上设置了自己的Apache服务器(XAMPP)。我写了两个php脚本;一个用于在Memcache中存储数据,另一个用于从Memcache中获取数据。Android设备正在使用HttpClient调用脚本。我使用memcache来消除写入数据库的一些延迟。下面是用于发送和接收数据的代码。。。对于测试,它是一个带有线程延迟的简单循环(计数器)。如果我将延迟设置为1秒,textview将更新并平稳运行。当我将延迟降低到0.1秒时,它会正常运行几秒钟,然后会结巴(暂停)3-5秒钟。它在运行时继续间歇暂停的过程。如果我将延迟降至0.01秒,系统将崩溃。我的Apache服务器直接连接到我的路由器。Android设备已连接到我的WiFi。我的问题是我是否在做一些根本错误的事情。我认为使用memcache,我可以轻松地对数据进行0.01秒的采样。我的代码是否正确处理异步任务?我怎样才能防止安卓系统显然因新任务而“过载”?好像我需要一个方法来确定任务何时完成,以便调用下一个任务。非常感谢您对php、httpclient等的反馈。。。任何影响性能的东西
编辑:当我将延迟降低到0.01秒时,我的系统不再崩溃。现在,我在onpostexecute方法中捕获了null值。然而,我还是会口吃/僵硬
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;
}
# 1 楼答案
取消startsendingdata()和startgettingdata()。在任务的onPostExecute()中,启动下一个任务的开始。现在,在开始新任务之前,您可以确定任务已完成。例如,您可以启动一个计时器来执行下一个任务