在后台从 android 执行 .php 文件的最佳方式是什么? 我需要它来刷新 mysql 数据库中的用户 Activity 时间。 因此,如果用户单击按钮,则应在数据库中使用他最后的 Activity 数据时间完成一个新条目。
我应该像这样使用异步任务吗?
private class UpdateUser extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
//I don't like to have a dialog. This hinds the user while he is using the app
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
jsonParser = new JSONParser();
String BENUTZER_NAME = actual_tUsername;
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("BENUTZER_NAME", BENUTZER_NAME));
//Call the php file defined as string which is updating the users last online datetime
JSONObject json = jsonParser.makeHttpRequest(url_update_activetime,
"POST", params);
if(json == null){
Log.v("NULL", "NULL");
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
}
}
或者我应该更好地使用runOnUiThread? 或者有更好的方法来解决这个问题吗?
提前致谢!
最佳答案
永远不要在 UI 线程上进行网络调用(我相信从 android 4.0 开始,它会抛出错误)。始终在单独的线程中执行此操作(AsyncTask 可以工作)。
关于php - 在后台多次从 android 执行 .php 文件的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19052966/