无法连接在web服务在android

问题描述:

我是webservices.new新我试图访问SQL数据从Android应用程序使用webservices。我的代码是给定的无法连接在web服务在android

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) 

Can有人请帮我解决这个问题。如果还有其他更好的方法与SQL Server连接。那么请给我建议。

+0

在doInBackground方法中放置Log.d()而不是Toast。 – Nirmal

+0

@Nirmal我已经检查使用Log.d()但没有效果。例外是在这条线之前。 – geeta

+0

检查我的答案。我已在下面发布。 – Nirmal

更改此

// 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); 
     } 
+0

我已经使用了'final SoapPrimitive result =(SoapPrimitive)envelope.getResponse();'并将URL改为'“http://10.0.2.2:10637/Service1.asmx?WSDL”'并禁用防病毒和防火墙。现在我能够从模拟器上的SQL获取数据,但是在物理设备上运行应用程序时无法与SQL连接。 – geeta

+0

我也尝试将URL改为'“http://192.168.1.64:10637/Service1.asmx?WSDL”'AND'“http://192.168.1.64:8080/Service1.asmx?WSDL”',但可用于在Android手机上连接。 – geeta

+0

还是你得到严格的模式错误! – Nirmal

当前异常解决!

之后将发生在你的代码 唯一的例外,因为你正在使用Toast UI线程里面涉及到UI线程

所以从以下

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

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

 runOnUiThread(new Runnable() { 

      @Override 
      public void run() { 

      } 

     }); 
     } 
+0

我评论吐司然后同样的错误即将到来。 – geeta

+0

异常将相同!但是当你解决当前的异常,那么这个异常就会发生! –