2013-06-05 107 views
0

我已經使用.Net C#創建了Web服務。現在我試圖從Android應用程序訪問這個,所以我嘗試使用KSOAP2。使用.Net Web服務的Android KSOAP

這是我的代碼。

final String NAMESPACE = "http://tempuri.org/"; 
final String METHOD_NAME = "HelloWorld"; 
final String SOAP_ACTION = "http://tempuri.org/HelloWorld"; 
final String URL = "http://localhost:61252/Service1.asmx"; 

new Thread() { 

public void run() { 
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
        SoapEnvelope.VER11); 
        envelope.dotNet=true; 
        envelope.setOutputSoapObject(request); 
        HttpTransportSE httpTransport = new HttpTransportSE(URL); 
        httpTransport.debug = true; 
        try { 
         httpTransport.call(SOAP_ACTION, envelope); 
         SoapObject result = (SoapObject) envelope.bodyIn; 

        } 
        catch (Exception e) { 
         // TODO: handle exception 
        }}}.start(); 

但在這一行

httpTransport.call(SOAP_ACTION, envelope); 

它進入catch塊,並沒有任何錯誤消息。

我也發現了很多關於這方面的問題,但是因爲我對這個新手無法找到我錯在哪裏。

我是否正確或有任何其他方法可以使用?

+1

in catch寫這行'e.printStackTrace();'讓我們知道它顯示哪個錯誤日誌。 – Lucifer

+0

嘗試調試它,並在try和catch塊中設置斷點。並檢查您的回覆或例外情況,並向我們提供日誌。 –

+0

謝謝,我試過e.printStackTrace()問題與互聯網連接,現在沒有添加互聯網使用權限,它正在工作。 – Giri

回答

1
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);   
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    envelope.setOutputSoapObject(request); 

    HttpTransportSE ht = new HttpTransportSE(URL); 
    ht.call(SOAP_ACTION, envelope); 
    final SoapPrimitive response = (SoapPrimitive)envelope.getResponse(); 
    final String str = response.toString(); 

將此用於您的reference。希望這會幫助你。

+0

謝謝..您提供的參考資料很有幫助。 – Giri

+0

@Giri繼續前進。快樂編碼.. – Nirmal

3
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
// if required you can use addProperty to add properties in url 
      request.addProperty("User", "[email protected]"); 
      request.addProperty("Password", "abcd"); 
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
        SoapEnvelope.VER11); 
      envelope.headerOut = new Element[1]; 
      envelope.headerOut[0] = buildAuthHeader(); 
      envelope.dotNet = true; 
      envelope.setOutputSoapObject(request); 

      // if you want to add any property you can add here. 

      /* 
      * PropertyInfo cityProp = new PropertyInfo(); 
      * 
      * cityProp.setType(String.class); request.addProperty(cityProp); 
      */ 

      Log.e("value of request", request.toString()); 
      Log.e("Value of envolope ", envelope.toString()); 

      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

    // add try catch block. 
      try { 

       androidHttpTransport.call(SOAP_ACTION, envelope); 
       Log.i("myAppEnvelope", envelope.toString()); 

       SoapObject response = (SoapObject) envelope.getResponse(); 
    // There are two types of Soap response (as per my knowledge or use) 
    // SoapObject or SoapPrimitive so check what is your response and use accordingly. 

    SoapObject data = (SoapObject) response 
         .getProperty("field_name"); 

    // OR 

    SoapPrimitive data = (SoapPrimitive) data.getProperty("field_name");