1

我一直在尝试使用 ksoap2 通过 android 调用一个简单的 .NET web 服务(HelloWorld)(我已经尝试过并成功用于不同的 web 服务)。但这需要身份验证,所以我搜索了如何为身份验证添加标头,但是我的文本视图中仍然没有返回结果。

SoapObject Request = new SoapObject(NAMESPACE,METHOD_NAME);  
List<HeaderProperty> headers = new ArrayList<HeaderProperty>();
headers.add(new HeaderProperty("Authorization", "Basic"+Base64.encode("Username:Password".getBytes())));


        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet=true;
        envelope.setOutputSoapObject(Request);

        HttpTransportSE aht = new HttpTransportSE(URL);


        try{
            aht.call(SOAP_ACTION,envelope,headers);
            SoapPrimitive resultString = (SoapPrimitive)envelope.getResponse();

            tv.setText("yo :" + resultString);

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

我的日志中也有错误:

错误:附加线程失败,但我不认为这是

这是 HelloWorld 方法的 wsdl:

>     <wsdl:types>
>     <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
>     <s:element name="HelloWorld">
>     <s:complexType/>
>     </s:element>
>     <s:element name="HelloWorldResponse">
>     <s:complexType>
>     <s:sequence>
>     <s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string"/>
>     </s:sequence>
>     </s:complexType>
>     </s:element>

我的问题。有没有人遇到过这种问题,或者我的代码在某些时候是错误的?

4

2 回答 2

0

.这就是我访问.net Web 服务的方式。在我的情况下,我发送 2 个参数用户名和密码,服务将根据数据库返回一个字符串。如果您没有传递任何参数,请删除我提到的参数并只添加这个

PropertyInfo pi = new PropertyInfo();
    pi.setType(String.class);
    request.addProperty(pi);

别的

 public String Call(String username, String password) {
    SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
            OPERATION_NAME);

    PropertyInfo pi = new PropertyInfo();
    pi.setName("username");
    pi.setValue(username);
    pi.setType(String.class);
    request.addProperty(pi);

    pi = new PropertyInfo();
    pi.setName("password");
    pi.setValue(password);
    pi.setType(String.class);
    request.addProperty(pi);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);

    envelope.dotNet = true;

    envelope.setOutputSoapObject(request);

    HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
    Object response = null;

    try {

        httpTransport.call(SOAP_ACTION, envelope);
        response = envelope.getResponse();
    }

    catch (Exception exception) {
        response = exception.toString();
    }

    return response.toString();
}
于 2013-02-18T11:45:10.097 回答
0

尝试这个:

 SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);    
request.addProperty("requst_name",request_value);//if you have any request add here..                   
 envelope.setOutputSoapObject(request);
 envelope.implicitTypes = true;
 envelope1.dotNet = true;
 int Timeout = 60000;
 HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, Timeout);
 androidHttpTransport.debug = true;
                try {

                    androidHttpTransport.call(SOAP_ACTION, envelope);
                    Object response = envelope.getResponse();
                    String res=response.toString();
                    }

                    }catch (XmlPullParserException e) {

                        e.printStackTrace();
                    } catch (SocketTimeoutException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
于 2013-02-18T11:31:34.723 回答