0

我正在尝试通过mysql服务器验证用户名,密码,相同的代码如下:

我已经在 php 中创建了 Web 服务以及我提到的命名空间和其他值,如下面的代码所示:

   package com.example.androidloginexampleactivity;

    import java.io.IOException;

    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.PropertyInfo;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapPrimitive;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;
    import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.os.StrictMode;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;

    public class AndroidLoginExampleActivity extends Activity {
        private TextView textView;
            /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_android_login_example);
            Button login = (Button) findViewById(R.id.btn_login);
            //this.accessWebService(textView);
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            login.setOnClickListener(new View.OnClickListener() {

                public void onClick(View arg0) {

                    LoginAction lgn = new LoginAction();
                    lgn.execute();

                                }
            });

        }


        private class LoginAction extends AsyncTask<String, Void, String>
        {
            protected String doInBackground(String... urls)
             {
                String webResponse = "";
                try{
                final String NAMESPACE = "urn:stockserver";
                final String URL = "http://localhost/stockserver1.php?wsdl";

                //private final String URL = " http://10.0.2.2:8080/WebApplication2/Login?Tester";
                final String SOAP_ACTION = "urn:stockserver#getStockQuote";
                final String METHOD_NAME = "getStockQuote";

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            EditText userName = (EditText) findViewById(R.id.tf_userName);
            String user_Name = userName.getText().toString();
            EditText userPassword = (EditText) findViewById(R.id.tf_password);
            String user_Password = userPassword.getText().toString();

            //Pass value for userName variable of the web service
            PropertyInfo unameProp =new PropertyInfo();
            unameProp.setName("userName");//Define the variable name in the web service method
            unameProp.setValue(user_Name);//set value for userName variable
            unameProp.setType(String.class);//Define the type of the variable
            request.addProperty(unameProp);//Pass propert   ies to the variable

            //Pass value for Password variable of the web service
            PropertyInfo passwordProp =new PropertyInfo();
            passwordProp.setName("password");
            passwordProp.setValue(user_Password);
            passwordProp.setType(String.class);
            request.addProperty(passwordProp);

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

            androidHttpTransport.call(SOAP_ACTION, envelope);
                SoapPrimitive response = (SoapPrimitive)envelope.getResponse();

                TextView result1 = (TextView) findViewById(R.id.tv_status);
                result1.setText(response.toString());

                }
            catch(Exception e)
            {
                Toast.makeText(getApplicationContext(),"Completed...", Toast.LENGTH_LONG).show();

            }   

            return webResponse; 
        }
            protected void onPostExecute(String result) {
               textView.setText(result);
               Toast.makeText(getApplicationContext(),"Completed...", Toast.LENGTH_LONG).show();
                }
    }

我收到以下错误:

   10-08 12:51:28.639: W/dalvikvm(2245): threadid=13: thread exiting with uncaught exception (group=0x40a71930)
   10-08 12:51:28.749: E/AndroidRuntime(2245): FATAL EXCEPTION: AsyncTask #3
   10-08 12:51:28.749: E/AndroidRuntime(2245): java.lang.RuntimeException: An error occured while executing doInBackground()
   10-08 12:51:28.749: E/AndroidRuntime(2245):  at android.os.AsyncTask$3.done(AsyncTask.java:299)
   10-08 12:51:28.749: E/AndroidRuntime(2245):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
   10-08 12:51:28.749: E/AndroidRuntime(2245):  at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
   10-08 12:51:28.749: E/AndroidRuntime(2245):  at java.util.concurrent.FutureTask.run(FutureTask.java:239)
   10-08 12:51:28.749: E/AndroidRuntime(2245):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
   10-08 12:51:28.749: E/AndroidRuntime(2245):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
   10-08 12:51:28.749: E/AndroidRuntime(2245):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
   10-08 12:51:28.749: E/AndroidRuntime(2245):  at java.lang.Thread.run(Thread.java:856)
   10-08 12:51:28.749: E/AndroidRuntime(2245): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
   10-08 12:51:28.749: E/AndroidRuntime(2245):  at android.os.Handler.<init>(Handler.java:197)
   10-08 12:51:28.749: E/AndroidRuntime(2245):  at android.os.Handler.<init>(Handler.java:111)
   10-08 12:51:28.749: E/AndroidRuntime(2245):  at android.widget.Toast$TN.<init>(Toast.java:324)
   10-08 12:51:28.749: E/AndroidRuntime(2245):  at android.widget.Toast.<init>(Toast.java:91)
   10-08 12:51:28.749: E/AndroidRuntime(2245):  at android.widget.Toast.makeText(Toast.java:238)
   10-08 12:51:28.749: E/AndroidRuntime(2245):  at com.example.androidloginexampleactivity.AndroidLoginExampleActivity$LoginAction.doInBackground(AndroidLoginExampleActivity.java:92)
   10-08 12:51:28.749: E/AndroidRuntime(2245):  at com.example.androidloginexampleactivity.AndroidLoginExampleActivity$LoginAction.doInBackground(AndroidLoginExampleActivity.java:1)
   10-08 12:51:28.749: E/AndroidRuntime(2245):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
   10-08 12:51:28.749: E/AndroidRuntime(2245):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
   10-08 12:51:28.749: E/AndroidRuntime(2245):  ... 4 more

请帮我解决这个问题,在此先感谢....

4

3 回答 3

1

您收到错误是因为您尝试执行需要从AsyncTaskdoInBackground() 方法访问 UI 线程的操作。例如,您看到在 doInBackground() 方法的最后,您尝试将文本设置在TextView. 这必须在 UI 线程上完成。

您应该做的是将需要 UI 威胁的所有内容移动 - 例如从 UI 元素设置/获取值到 onPostExecute。这在 UI 线程上运行,应该可以解决问题。

于 2013-10-08T13:12:31.353 回答
0

尝试在 AsyncTask 的 doInBackground 方法中编写以下两行

Looper.myLooper();
Looper.prepare();

还要在 Handler 中做所有的 UI 事情。

于 2013-10-08T13:19:29.530 回答
0

您收到 looper.prepare 错误,因为您正在从异步任务中调用 toast。从 onPostExecute() 甚至 doInBackground() 中删除吐司(这是引发错误的那个)..出于跟踪目的,您可以使用 Log.i 方法

于 2013-10-08T13:21:04.193 回答