簡體   English   中英

帶有.Net Web服務的Android KSOAP

[英]Android KSOAP with .Net Web Service

我已經使用.Net C#創建了Web服務。 現在,我嘗試從android應用程序訪問此文件,因此嘗試使用KSOAP2。

這是我的代碼。

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塊,並且沒有任何錯誤消息。

我也發現了很多與此有關的問題,但是由於我是新來的,所以找不到我要去哪里。

我做對了還是有其他可以使用的方法?

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// if required you can use addProperty to add properties in url
            request.addProperty("User", "abcd@gmail.com");
            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");
 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();

以此為參考 希望這會幫助你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM