2013-08-05 147 views
3

我與Visual Studio 2010 WCF創建和webmaterix 3發佈.... 我把這個Web服務在Android應用程序,但沒有運行... 這是我的web.config:調用web服務與KSOAP

<?xml version="1.0"?> 
<configuration> 
<system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
</system.web> 
<system.serviceModel> 
    <behaviors> 
    <serviceBehaviors> 
    <behavior> 

     <serviceMetadata httpGetEnabled="true"/> 

     <serviceDebug includeExceptionDetailInFaults="false"/> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
</system.serviceModel> 
<system.webServer> 
<modules runAllManagedModulesForAllRequests="true"/> 
</system.webServer> 

</configuration> 

,這在我的Android代碼:

private static final String NAMESPACE = "http://tempuri.org/"; 
      private static final String URL = 
       "http://localhost:60977/Service1.svc?singleWsdl"; 
    private static final String SOAP_ACTION =  "http://tempuri.org/IService1/GetData"; 
      private static final String METHOD_NAME = "GetData"; 
      TextView txt=(TextView)findViewById(R.id.textView1); 
     SoapObject request = new SoapObject(NAMESPACE,  METHOD_NAME);  
    SoapSerializationEnvelope envelope = 
     new SoapSerializationEnvelope(SoapEnvelope.VER11); 

       envelope.setOutputSoapObject(request); 
     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
       name("SS"); 
       try { 
        name("0"); 
      androidHttpTransport.call(SOAP_ACTION, envelope); 
        name("1"); 
     SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn; 
        name("2"); 
        name(resultsRequestSOAP.toString()); 
        name("3"); 
      txt.setText("Received :" + resultsRequestSOAP.toString()); 

       } catch (Exception e) { 
        txt.setText(e.getMessage()); 
        name(e.getMessage()); 
        e.printStackTrace(); 
       } 

時抓運行,沒有任何消息......

+0

在AVD連接到本地主機時,您需要使用url http://10.0.2.2/而不是http:// localhost / – Nirmal

回答

0
private static final String URL = 
       "http://localhost:60977/Service1.svc?singleWsdl"; 
      private static final String METHOD_NAME = "GetData"; 
    final String SOAP_ACTION = "http://tempuri.org/GetData";     

         try { 
          SoapObject request = new SoapObject(WebServiceUtil.NAMESPACE, METHOD_NAME); 
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
          envelope.dotNet = true;       
          envelope.setOutputSoapObject(request); 

          HttpTransportSE androidHttpTransport = new HttpTransportSE(WebServiceUtil.URL); 
          Log.d("URL :: ", WebServiceUtil.URL);   
          androidHttpTransport.call(SOAP_ACTION, envelope); 


         } 
         catch (Exception e) { 
          e.printStackTrace(); 

         } 

       } 
0

實際上,本地主機是模擬器本身在這裏,因爲代碼仿真器中運行。所以你應該連接到10.0.2.2。有關更多詳細信息,請參閱 Android Emulator Networking並且不要忘記清單文件中的網絡權限