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

        }

       }

        }

LOGCAT

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 线程中运行的方法之一中完成,也许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 ()中Toast运行sendtoserver()时显示此问题。

相反,我建议您重新安排代码并从doInBackground()返回任何错误代码并显示在onPostExecute()内部- 它在UI thread上运行。Toast

或者您可以使用在UI 线程上运行的runOnUiThread方法。Runnable

于 2012-10-02T19:39:11.587 回答