无法连接android中的webservice

时间:2013-07-15 06:49:58

标签: android sql-server web-services ksoap2

我是webservices中的newbei。我正在尝试使用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。那么请建议我。

2 个答案:

答案 0 :(得分: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);
        }

答案 1 :(得分:0)

当前的异常解决了!

之后

您的代码中将出现的一个例外与 UI线程

相关

因为您在UI线程中使用Toast

所以从以下

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

或者如果您想使用Toast而不是将其与runOnUiThread

一起使用
      runOnUiThread(new Runnable() {

            @Override
            public void run() {

            }

        });
      }