2

你们能检查我的代码吗,我不知道它是否构造正确,我需要在 doInBackGround 过程完成后显示 toast 并运行一个类。好吧,它不起作用,所以你们可以在这里帮助我。谢谢。

这是我的代码:

**

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);
             }

        }

**

4

2 回答 2

1

将 Context 传递给此 AsyncTask 的构造函数。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();
        [...]
    }

编辑:还在你的 if 子句中添加一个 NullPointer 保护。就像是:

if(result != null && result.equalsIgnoreCase("1")){
于 2013-01-28T06:56:48.160 回答
0
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");
     }
 }

有关更多信息,请查看此链接

于 2013-01-28T07:21:50.320 回答