2

私のコードをチェックしてもらえますか。コードが適切に構築されているかどうかわかりません。トーストを表示して、doInBackGround プロセスが完了した後にクラスを実行する必要があります。うまくいかないので、ここで私を助けてくれませんか。ありがとう。

これは私のコードです:

**

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

この AsyncTask のコンストラクターに Context を渡します。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 に答える