Android 异步post不工作后的Sqlite数据库更新
我使用HttpPost方法通过web服务器从android设备发送消息。当响应返回时,设备应该用我使用过一百万次的格式更新车载数据库Android 异步post不工作后的Sqlite数据库更新,android,sqlite,sql-update,Android,Sqlite,Sql Update,我使用HttpPost方法通过web服务器从android设备发送消息。当响应返回时,设备应该用我使用过一百万次的格式更新车载数据库 if(postMessage(theMessage).trim().equals("OK")){ DatabaseHelper helper = new DatabaseHelper(ShareDialog.this); database = helper.getWritableDatabase(); strSharedWith = strShare
if(postMessage(theMessage).trim().equals("OK")){
DatabaseHelper helper = new DatabaseHelper(ShareDialog.this);
database = helper.getWritableDatabase();
strSharedWith = strSharedWith + theNameEncoded+ "|~|";//my delimiter;
String strFilter = "listTitle= '" + listTitle + "'";
ContentValues values = new ContentValues();
values.put("sharedWith", strSharedWith);
int numRows = database.update("listdata", values, strFilter, null);
database.close();
Toast.makeText(getBaseContext(), numRows + "", Toast.LENGTH_SHORT).show();//Toasts "1"
一切似乎都正常:消息被发送到另一个设备,响应从web服务器返回,Toast消息指示一行已更新……但值仍然为空。有人看到我遗漏了什么吗?谢谢
编辑:我发现,如果在调用AsyncTask之前进行查询,则查询可以正常工作……对于我来说,查询没有执行似乎很奇怪,因为我知道if块没有执行(因为有一个else块显然没有执行)。冒着让自己难堪的风险,我发布了异步任务的代码;我知道这个词的用法
while (something.equals("")){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这是一种糟糕的做法,但这是我将帖子发送到服务器的方式。我现在看到我最好清理我的第二个线程使用情况,所以如果有人可以这样做(并告诉我在哪里更新本地数据库),我会将它标记为已回答。哦,旁注:建议使用execSQL生成的异常,因为我的管道分隔符转义不当——我使用的格式避免了这一点。好的,红脸:
if(postMessage(theNameEncoded).trim().equals("OK")){
//was making my database update here, which didn't work
m_adapter.notifyDataSetInvalidated();
loadAdapter();
setListAdapter(m_adapter);
}else{
//alert dialog
}
public String postMessage(String toWhom){
//get stuff from database
strToPost = (assembled from the database call);
new MyAsyncTask().execute(strToPost);
while (privateStatus.equals("")){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (privateStatus.indexOf("OK")>-1){
//the return from the webserver, OK on success
return "OK";
}else{
return "Not a friend";
}
还有,异步任务
private class MyAsyncTask extends AsyncTask<String, Integer, Double>{
protected void onPreExecute() {
//progress dialog started
}
protected Double doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.my-domain.com/list_post.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("listSent", strToPost));
nameValuePairs.add(new BasicNameValuePair("fromUser", myName));
nameValuePairs.add(new BasicNameValuePair("toUser", sharingListWith));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
status = httpclient.execute(httppost, new BasicResponseHandler());
while (status.equals("")){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
setStatus(status);
//exception catching
protected void onPostExecute(Double result){
pDialog.dismiss();
}
}
public String setStatus (String theStatus){
if(!theStatus.equals(null)){
privateStatus = theStatus;
}
return null;
}
私有类MyAsyncTask扩展了AsyncTask{
受保护的void onPreExecute(){
//“进度”对话框已启动
}
受保护的双doInBackground(字符串…参数){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://www.my-domain.com/list_post.php");
试一试{
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“listSent”,strToPost));
添加(新的BasicNameValuePair(“fromUser”,myName));
添加(新的BasicNameValuePair(“toUser”,sharingListWith));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
status=httpclient.execute(httppost,newbasicResponseHandler());
while(status.equals(“”){
试一试{
睡眠(1000);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
设置状态(状态);
//异常捕获
受保护的void onPostExecute(双重结果){
pDialog.disclose();
}
}
公共字符串集合状态(字符串集合状态){
如果(!theStatus.equals(null)){
私有状态=状态;
}
返回null;
}
我知道。还有两个是在{sleep}的时候。如果您能就如何将Web服务器的响应正确地合并到辅助线程中提出建议,我将不胜感激。我只能在调用AsyncTask之前通过调用update函数来更新本地数据库;我使用了ConnectionManager(如上面链接中所述)为了防止更新发生,如果HTTP POST不会发生。我认为这是不够的,仍然不明白为什么我不能使用AcyCtaskOnTeXPART部分——在UI线程中,并且被从Web服务器的帖子中得到的结果,似乎是做这件事的地方。对此,我将不胜感激。同时,我会在我的应用程序运行时将此标记为已回答!我正在调查有关此主题的问题;问题似乎已在此处解决。我会随时向您通报。k伙计,我将删除我的回答:)