0

在后台从android执行.php文件的最佳方法是什么?我需要它来刷新 mysql 数据库中的用户活动时间。因此,如果用户单击一个按钮,则应在数据库中使用他的最后一个活动数据时间完成一个新条目。

我应该像那样使用异步任务吗?

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?或者有没有更好的方法来解决这个问题??

提前致谢!

4

1 回答 1

2

永远不要在 UI 线程上进行网络调用(我相信从 android 4.0 开始,它会引发错误)。始终在单独的线程中执行此操作(AsyncTask 将起作用)。

于 2013-09-27T14:58:52.077 回答