Java mysql:使用循环从mysql更新android变量
我尝试用mysql数据库中的数据更新字符串变量Java mysql:使用循环从mysql更新android变量,java,android,mysql,Java,Android,Mysql,我尝试用mysql数据库中的数据更新字符串变量 例如: String str应该使用php从mysql数据库中获取一个值。这是做了,工作完美 但是str应该每10秒获取一次这些值,所以它必须在循环中运行 这就是问题所在 通过httppost连接并获取实体会阻塞UI,因此会跳过帧。 为了解决这个问题,我使用了:服务、异步任务和可运行文件,但总是跳帧 这是我最近尝试的一件事: public class AsyncStatus extends AsyncTask<String, String,
例如: String str应该使用php从mysql数据库中获取一个值。这是做了,工作完美 但是str应该每10秒获取一次这些值,所以它必须在循环中运行 这就是问题所在 通过httppost连接并获取实体会阻塞UI,因此会跳过帧。 为了解决这个问题,我使用了:服务、异步任务和可运行文件,但总是跳帧 这是我最近尝试的一件事:
public class AsyncStatus extends AsyncTask<String, String, String>{
HttpResponse response;
String str;
HttpPost httppost;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
Handler mHandler=null;
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
//Build the connection only once, to save performance
getStrFirst();
//Loop started
final Runnable mUpdateUI = new Runnable() {
public void run() {
System.out.println("called");
try {
//to avoid "already consumed exceptions"
response = httpclient.execute(httppost);
//get the content
str = EntityUtils.toString(response.getEntity());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
str = str.replaceAll("\\D", "");
mHandler.postDelayed(this, 10000); // 10 seconds
}
};
mHandler.post(mUpdateUI);
return null;
}
public void getStrfirst(){
nameValuePairs = new ArrayList<NameValuePair>();
try
{
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://lunation.square7.ch/msqlcount.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
str = EntityUtils.toString(response.getEntity());
编辑:
我使用以下命令调用AsyncTask:
AsyncStatus assi= new AsyncStatus();
assi.execute("nothing");
为什么会出现此消息,这不是主线程
感谢您提供的任何建议请说明如何调用AsyncTask。试试这个
AsyncStatus assi= new AsyncStatus();
assi.execute("nothing");