2012-03-01 67 views
0

我有以下WCF webservice消費通過KSOAP2 Android上的WCF服務給錯誤

我想使用它在android系統client.My代碼到目前爲止是

public class SOAP_TestActivity extends Activity { 

    private final String NAMESPACE = "http://tempuri.org/"; 
    private final String URL = "http://50.19.226.15/Office365Admin/MsOnline.svc"; 
    private final String SOAP_ACTION = "http://tempuri.org/IMsOnline/ValidateUser"; 
    private final String METHOD_NAME = "ValidateUser"; 


    private final String user_id= "[email protected]"; 
    private final String password= "[email protected]"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
     { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Button signin = (Button) findViewById(R.id.regsubmitbtn); 
     signin.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 

         boolean auth=doLogin(); 
         System.out.println(auth); 
         Toast.makeText(getApplicationContext(), String.valueOf(auth), Toast.LENGTH_SHORT).show(); 

       } 
      }); 
    } 

    private boolean doLogin() { 

    boolean result=false; 

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

    request.addProperty("userid", user_id); 
    request.addProperty("password",password); 

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

    envelope.dotNet = true;   

    envelope.setOutputSoapObject(request); 

    Log.d("myApp", request.toString()); 
    System.out.println(request); 

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
    androidHttpTransport.debug = true; 

    try { 
      androidHttpTransport.call(SOAP_ACTION, envelope); 

      SoapPrimitive soapPrimitiveResults = (SoapPrimitive)envelope.getResponse();       

      Log.d("myApp", soapPrimitiveResults.toString()); 
      System.out.println("myApp" +soapPrimitiveResults); 

        if(soapPrimitiveResults.toString().equalsIgnoreCase("true")) 
     { 
        result = true; 

     } 

    }catch(SocketException ex) 
    { 
     Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage()); 
     ex.printStackTrace(); 
    } 
    catch (Exception e) { 
     Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage()); 
      e.printStackTrace(); 
    } 
    return result; 

    } 
    } 

此代碼的工作沒有任何異常,但我我無法登錄到我的應用程序,因爲方法ValidateUser總是返回false.This相同的Web服務已在Windows Phone中使用,並且沒有任何問題。而且用戶名和密碼是100%正確的。

看起來在NAMESPACESOAP_ACTION中存在一些問題。我已經經歷了一些教程走了,他們說,SOAP_ACTION通過連接NAMESPACEMETHOD_NAME。所以,如果我按照教程我SOAP_ACTION應該

"http://tempuri.org/ValidateUser"

製作,但在WSDL link的方法"ValidateUser"給出的SOAP_ACTION

"http://tempuri.org/IMsOnline/ValidateUser"

我是新來的WCF Webservices和SOAP.Can任何人請告訴我如何實現WSDL Document(link is at the top)中給出的方法。 看起來像缺少重要的東西,但不知道是什麼。

回答

0

試試這個:

... 

Object response = envelope.getResponse(); 
String value = response.toString(); 

... 
1

如果同一個web服務工作正常的Windows Phone,那麼你有一個命名空間的問題, 和Web服務方法沒有得到來自SOAP消息的參數。而且由於你的ValidateUser沒有獲取參數,它只是返回null/false。

我有同樣的問題。我解決它通過顯式定義命名空間,因此擺脫了默認的命名空間WCF套,tempuri.org

這裏是我的參考解決方案:

passing objects to wcf soap service from android using ksoap2; it sends and receives 0