2

これが私のコードです。送信ボタン内で Asynctask を呼び出し、doInBackground メソッド内で Web サービス コードを実装している sendtoserver メソッドを呼び出しています。

     package asynctask.com;

     import org.ksoap2.SoapEnvelope;
     import org.ksoap2.serialization.SoapObject;
     import org.ksoap2.serialization.SoapSerializationEnvelope;
     import org.ksoap2.transport.HttpTransportSE;



     import android.os.AsyncTask;
     import android.os.Bundle;
     import android.app.Activity;
     import android.app.ProgressDialog;
     import android.util.Log;
     import android.view.Menu;
     import android.view.MenuItem;
     import android.view.View;
     import android.view.Window;
     import android.view.WindowManager;
     import android.view.View.OnClickListener;
     import android.widget.Button;
     import android.widget.DatePicker;
     import android.widget.EditText;
     import android.widget.Spinner;
     import android.widget.Toast;
     import android.support.v4.app.NavUtils;

        public class Asyntask extends Activity {
         boolean resultofmail;
    Button send;
    private static final String SOAP_ACTION = "http://sendmail.com/insertfeedback";
        private static final String METHOD_NAME = "insertfeedback";
        private static final String NAMESPACE = "http://sendmail.com/";
        private static final String URL = "http://180.179.48.27:8080/sendmail      /sendmailService?wsdl";


       EditText ename;
       EditText eid,reason;
       DatePicker sdate,edate;
       Spinner rfl;




         @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.leaverequest);
        send=(Button)findViewById(R.id.button1);

        eid=(EditText)findViewById(R.id.editText1);
        ename=(EditText)findViewById(R.id.editText2);

        sdate=(DatePicker)findViewById(R.id.datePicker1);
        edate=(DatePicker)findViewById(R.id.datePicker2);
        rfl=(Spinner)findViewById(R.id.spinner1);
        reason=(EditText)findViewById(R.id.editText3);



        send.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                new MailSendingTask().execute((Void[])null);


            }
        });



         }

        public boolean sendtoserver()
          {
        int day  =sdate.getDayOfMonth();
        int month=sdate.getMonth()+1;
        int year =sdate.getYear();
        String stdate=day+"/"+month+"/"+year;

        int day1  =edate.getDayOfMonth();
        int month1=edate.getMonth()+1;
        int year1 =edate.getYear();
        String eddate=day1+"/"+month1+"/"+year1;

                Toast.makeText(getApplicationContext(),stdate+"   "+eddate,Toast.LENGTH_LONG).show();



            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
                request.addProperty("name",ename.getText().toString());
                request.addProperty("code",eid.getText().toString());
                request.addProperty("date1",stdate);
                request.addProperty("date2",eddate);
                request.addProperty("leave",rfl.getSelectedItem().toString());
                request.addProperty("reason",reason.getText().toString());



                SoapSerializationEnvelope envelope = new   SoapSerializationEnvelope(SoapEnvelope.VER11); 
                envelope.setOutputSoapObject(request);
                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);


                 try {
                   androidHttpTransport.call(SOAP_ACTION, envelope);
                   Toast.makeText(getApplicationContext(),"Mail Sent",Toast.LENGTH_LONG).show();
                   return true;

                 }
                 catch (Exception e) {
                     Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
                     return false;

                    }
   }



    public class MailSendingTask extends AsyncTask <Void, Void, Void> {

        private ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {
            this.progressDialog = ProgressDialog.show(
                    Asyntask.this,
                    "Doing Processing", // title
                    "Sending Data To Server", // message
                    true // indeterminate
            );
        }

        @Override
        protected Void doInBackground(Void... params) {

            resultofmail=sendtoserver();
            return null;
        }

        @Override
        protected void onPostExecute(Void v) {
            this.progressDialog.cancel();
            if (resultofmail)
            Toast.makeText(getApplicationContext(), "Mail sent", Toast.LENGTH_LONG).show(); 
            else
                Toast.makeText(getApplicationContext(), "Retry", Toast.LENGTH_LONG).show(); 

        }

       }

        }

ログキャット

10-03 00:23:38.600: E/AndroidRuntime(1770): FATAL EXCEPTION: AsyncTask #1
10-03 00:23:38.600: E/AndroidRuntime(1770): java.lang.RuntimeException: An error occured while executing doInBackground()
10-03 00:23:38.600: E/AndroidRuntime(1770):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
10-03 00:23:38.600: E/AndroidRuntime(1770):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
10-03 00:23:38.600: E/AndroidRuntime(1770):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
10-03 00:23:38.600: E/AndroidRuntime(1770):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
10-03 00:23:38.600: E/AndroidRuntime(1770):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
10-03 00:23:38.600: E/AndroidRuntime(1770):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
10-03 00:23:38.600: E/AndroidRuntime(1770):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
10-03 00:23:38.600: E/AndroidRuntime(1770):     at java.lang.Thread.run(Thread.java:1019)
10-03 00:23:38.600: E/AndroidRuntime(1770): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
10-03 00:23:38.600: E/AndroidRuntime(1770):     at android.os.Handler.<init>(Handler.java:121)
10-03 00:23:38.600: E/AndroidRuntime(1770):     at android.widget.Toast.<init>(Toast.java:68)
10-03 00:23:38.600: E/AndroidRuntime(1770):     at android.widget.Toast.makeText(Toast.java:231)
10-03 00:23:38.600: E/AndroidRuntime(1770):     at asynctask.com.Asyntask.sendtoserver(Asyntask.java:89)
10-03 00:23:38.600: E/AndroidRuntime(1770):     at asynctask.com.Asyntask$ReverseGeocodeLookupTask.doInBackground(Asyntask.java:140)
10-03 00:23:38.600: E/AndroidRuntime(1770):     at asynctask.com.Asyntask$ReverseGeocodeLookupTask.doInBackground(Asyntask.java:1)
10-03 00:23:38.600: E/AndroidRuntime(1770):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-03 00:23:38.600: E/AndroidRuntime(1770):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
10-03 00:23:38.600: E/AndroidRuntime(1770):     ... 4 more
10-03 00:23:38.770: W/IInputConnectionWrapper(1770): showStatusIcon on inactive InputConnection
10-03 00:23:39.250: E/WindowManager(1770): Activity asynctask.com.Asyntask has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40565b30 that was originally added here
10-03 00:23:39.250: E/WindowManager(1770): android.view.WindowLeaked: Activity asynctask.com.Asyntask has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40565b30 that was originally added here
10-03 00:23:39.250: E/WindowManager(1770):  at android.view.ViewRoot.<init>(ViewRoot.java:259)
10-03 00:23:39.250: E/WindowManager(1770):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
10-03 00:23:39.250: E/WindowManager(1770):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
10-03 00:23:39.250: E/WindowManager(1770):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
10-03 00:23:39.250: E/WindowManager(1770):  at android.app.Dialog.show(Dialog.java:241)
10-03 00:23:39.250: E/WindowManager(1770):  at android.app.ProgressDialog.show(ProgressDialog.java:107)
10-03 00:23:39.250: E/WindowManager(1770):  at android.app.ProgressDialog.show(ProgressDialog.java:90)
10-03 00:23:39.250: E/WindowManager(1770):  at asynctask.com.Asyntask$ReverseGeocodeLookupTask.onPreExecute(Asyntask.java:129)
10-03 00:23:39.250: E/WindowManager(1770):  at android.os.AsyncTask.execute(AsyncTask.java:391)
10-03 00:23:39.250: E/WindowManager(1770):  at asynctask.com.Asyntask$1.onClick(Asyntask.java:67)
10-03 00:23:39.250: E/WindowManager(1770):  at android.view.View.performClick(View.java:2485)
10-03 00:23:39.250: E/WindowManager(1770):  at android.view.View$PerformClick.run(View.java:9080)
10-03 00:23:39.250: E/WindowManager(1770):  at android.os.Handler.handleCallback(Handler.java:587)
10-03 00:23:39.250: E/WindowManager(1770):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-03 00:23:39.250: E/WindowManager(1770):  at android.os.Looper.loop(Looper.java:123)
10-03 00:23:39.250: E/WindowManager(1770):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-03 00:23:39.250: E/WindowManager(1770):  at java.lang.reflect.Method.invokeNative(Native Method)
10-03 00:23:39.250: E/WindowManager(1770):  at java.lang.reflect.Method.invoke(Method.java:507)
10-03 00:23:39.250: E/WindowManager(1770):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
10-03 00:23:39.250: E/WindowManager(1770):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
10-03 00:23:39.250: E/WindowManager(1770):  at dalvik.system.NativeStart.main(Native Method)

注 - Web サービス コードは、Asynctask タスクがなくても正常に動作します。
注 - doInBackground メソッド内で直接時間のかかる計算を行う場合、Asynctask は正常に機能します。

doInBackground メソッド内でこれを試してみましたが、Webservice が機能して実装されていますが、現在は進行状況ダイアログで行き詰まりました。消えません。

if (Looper.myLooper() == null) { 
Looper.prepare(); 
} 
mLooper = Looper.myLooper(); resultofmail=sendtoserver(); 
Looper.loop(); 
return null; 
4

3 に答える 3

2

からトーストを作成しないでくださいdoInBackground。これらは UI でありAsyncTask、UI スレッドで実行されるメソッドの 1 つで実行する必要がありますonProgressUpdate

于 2012-10-02T19:39:00.730 に答える
2

あなたのトーストが問題の原因です! UI スレッドでトーストを作成するメソッドを作成できます。

例えば

public static void toastMSG (final String msg , int length )
    {
        activity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(context, msg, length).show();

            }
        });
    }

次に、Toast.makeText(...) の代わりにこのメソッドを使用します

于 2012-10-02T19:39:09.327 に答える
2

doInBackground()内でsendtoserver(Toast )を実行しているときに表示しようとすると、問題が発生します。

代わりに、コードを再配置し、doInBackground() からエラー コードを返し UIスレッド実行されるonPostExecute()内に表示することをお勧めします。Toast

または、UI スレッドで実行する runOnUiThread メソッドを使用することできます。Runnable

于 2012-10-02T19:39:11.587 に答える