0

我是 webservices 的新手。我正在尝试使用 webservices 从 Android App 访问 SQL 数据。我的代码是给定的

package sqlwebservice.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.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;



public class MainActivity extends Activity {

       //String SOAP_ACTION="http://tempuri.org/HelloWorld";
       //String METHOD_NAME = "HelloWorld";
        String NAMESPACE = "http://tempuri.org/";
        String URL = "http://192.168.1.64:8080/Service1.asmx?WSDL";
        String GetPass_SOAP_ACTION="http://tempuri.org/GetPassword";
        String METHOD_NAME1 = "GetPassword";

        EditText username,pass;
        Button btGetpass;

    String WebResponse;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         username = (EditText)findViewById(R.id.editText1);
            pass = (EditText)findViewById(R.id.editText2);
            btGetpass = (Button)findViewById(R.id.button1);

            btGetpass.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    WebSearchTask task = new WebSearchTask();
                    task.execute(WebResponse);


                }
            });
    }
    public class WebSearchTask extends AsyncTask<String, String, String> {  
         @Override  
         protected String doInBackground(String... params) {  

             SoapObject result = null;
                     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);

                    //Use this to add parameters
                    String user_name = username.getText().toString();
                    request.addProperty("Username",user_name);


                    //Declare the version of the SOAP request
                     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

                     envelope.setOutputSoapObject(request);
                     envelope.dotNet = true;

                     try {
                         HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,300000);

                         //this is the actual part that will call the webservice
                         androidHttpTransport.call(GetPass_SOAP_ACTION, envelope); // Exception is coming here

                         // Get the SoapResult from the envelope body.
                          result = (SoapObject)envelope.bodyIn;

                         if(result == null)
                         {
                               Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
                         }
                   } catch (Exception e) {
                         e.printStackTrace();
           }
              return result.getProperty(0).toString();  
         }  

         @Override
         protected void onPostExecute(String result) {
             pass.setText(result);
         }

    }  
}

当我调试这段代码时。我在这条线上遇到了异常

androidHttpTransport.call(GetPass_SOAP_ACTION, envelope); // Exception is coming here

例外如下。

    07-15 17:06:44.534: E/AndroidRuntime(16971): FATAL EXCEPTION: AsyncTask #1
07-15 17:06:44.534: E/AndroidRuntime(16971): java.lang.RuntimeException: An error occured while executing doInBackground()
07-15 17:06:44.534: E/AndroidRuntime(16971):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-15 17:06:44.534: E/AndroidRuntime(16971):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-15 17:06:44.534: E/AndroidRuntime(16971):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-15 17:06:44.534: E/AndroidRuntime(16971):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-15 17:06:44.534: E/AndroidRuntime(16971):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-15 17:06:44.534: E/AndroidRuntime(16971):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-15 17:06:44.534: E/AndroidRuntime(16971):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-15 17:06:44.534: E/AndroidRuntime(16971):    at java.lang.Thread.run(Thread.java:1096)
07-15 17:06:44.534: E/AndroidRuntime(16971): Caused by: java.lang.NullPointerException
07-15 17:06:44.534: E/AndroidRuntime(16971):    at sqlwebservice.com.MainActivity$WebSearchTask.doInBackground(MainActivity.java:95)
07-15 17:06:44.534: E/AndroidRuntime(16971):    at sqlwebservice.com.MainActivity$WebSearchTask.doInBackground(MainActivity.java:1)
07-15 17:06:44.534: E/AndroidRuntime(16971):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-15 17:06:44.534: E/AndroidRuntime(16971):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

任何人都可以帮我解决这个问题。如果有其他更好的方式来连接 SQL Server。那么请给我建议。

4

2 回答 2

1

改变这个

 // Get the SoapResult from the envelope body.
                      result = (SoapObject)envelope.bodyIn;

进入

  final SoapPrimitive result = (SoapPrimitive) envelope.getResponse();

使用以下代码解决StrictMode问题。

 if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        }
于 2013-07-15T07:47:47.950 回答
0

当前异常解决时!

之后, 您的代码中将发生的一个异常将与UI 线程有关

因为您Toast在 UI 线程内部使用

所以从关注中删除吐司

if(result == null)
  {
    Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
   }

或者如果你想使用Toast而不是使用它runOnUiThread

      runOnUiThread(new Runnable() {

            @Override
            public void run() {

            }

        });
      }
于 2013-07-15T07:05:34.827 回答