简体   繁体   English

在AsyncTask中敬酒

[英]Toast in AsyncTask

Can you guys check my codes, i don't know if it's constructed properly, i need to display toast and run a class after the doInBackGround process is done. 你们可以检查我的代码吗,我不知道它的结构是否正确,在doInBackGround过程完成后,我需要显示烤面包并运行一个类。 Well it's not working, so can you guys help me here. 好吧,它不起作用,所以你们可以在这里帮助我。 Thanks. 谢谢。

This is my codes: 这是我的代码:

**

class phpconnect extends AsyncTask<String, String, String>{
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Main.this);
            pDialog.setMessage("Logging in..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
             ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("eadd", inputEmail.getText().toString()));
                postParameters.add(new BasicNameValuePair("password", inputPassword.getText().toString()));
                //Passing Parameter to the php web service for authentication
                //String valid = "1";
                String response = null;
                try {
                response = CustomHttpClient.executeHttpPost("http://10.0.2.2:8080/TheCalling/log_in.php", postParameters);  //Enter Your remote PHP,ASP, Servlet file link
                String res=response.toString();
                //res = res.trim();
                res= res.replaceAll("\\s+","");
                //error.setText(res);

                } catch (Exception e) {
                    return "1";
                }
            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String result) {
            // dismiss the dialog once done
             pDialog.dismiss();
            if(result.equalsIgnoreCase("1")){
                //Display Toast
                Toast.makeText( getApplicationContext(),"Sorry!! Incorrect Username or Password",Toast.LENGTH_SHORT ).show();
             }else{
                    Toast.makeText( getApplicationContext(),"Correct Username or Password",Toast.LENGTH_SHORT ).show();
                    Intent i = new Intent(Main.this,MainMenu.class);
                    startActivity(i);
             }

        }

** **

Pass a Context to a constructor of this AsyncTask. 将上下文传递给此AsyncTask的构造函数。 An AsyncTask does not extend Context itself so you cannot use getApplicationContext() AsyncTask不会扩展Context本身,因此您不能使用getApplicationContext()

class phpconnect extends AsyncTask<String, String, String>{

    private final Context mContext;

    public phpconnect(final Context context) {
        mContext = context;
    }


    [...]

    protected void onPostExecute(String result) {
        [...]
        Toast.makeText( mContext,"...",Toast.LENGTH_SHORT ).show();
        [...]
    }

Edit: Also add a NullPointer guard to your if-clause. 编辑:还向您的if子句添加NullPointer防护。 Something like: 就像是:

if(result != null && result.equalsIgnoreCase("1")){
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

For more information check this link 有关更多信息,请检查此链接

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

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