简体   繁体   English

PHP和android之间的通信

[英]communication between PHP and android

我正在开发一个Android应用程序,其中用户将数据发送到PHP服务器。为此,我正在使用REST Web服务,我正在发送大量的数据,这需要花费近2分钟的时间来执行,并且我希望用户将数据发送到PHP服务器时,服务器应该发送成功消息给用户和服务器在单独的线程中执行数据。这怎么可能?

You can use AsyncTask to perform heavy operations without blocking the UI thread. 您可以使用AsyncTask执行繁重的操作而不会阻塞UI线程。

I believe this code should be enough to get you started. 我相信这段代码应该足以让您入门。

private class MyHeavyTask extends AsyncTask<Void, Void, Boolean>{
    // Perform initialization here. 
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        // Showing progress dialog
        progressDialog = new ProgressDialog(MyActivity.this);
        progressDialog.setMessage("Loading...");
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    // If an error is occured executing doInBackground()
    @Override
    protected void onCancelled() {
        if (progressDialog.isShowing())
            progressDialog.dismiss();
        // Show an alert box.
        AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);

        builder.setCancelable(false)
               .setTitle("Error!")
               .setMessage("Error in executing your command.")
               .setInverseBackgroundForced(true)
               .setPositiveButton("Dismiss", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }

    @Override
    protected Boolean doInBackground(Void... arg0) {
        if(!isCancelled()) {
            // Perform heavy lifting here.
        }
        return true;
    }

    // After the request's performed. Here, hide the dialog boxes, inflate lists, etc.
    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (progressDialog.isShowing())
            progressDialog.dismiss();
    }

}

You can simply call the AsyncTask via the following line of code: 您只需通过以下代码行调用AsyncTask:

new MyHeavyTask().execute();

Hope it helps with your problem. 希望对您的问题有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM