我一直试图通过android使用ksoap2调用一个简单的.NET webservice(HelloWorld)(我已经尝试并成功使用了不同的webservice)。但是这个需要身份验证,所以我搜索了如何为身份验证添加标头,但是,我的textview中没有返回任何结果。
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>
我的问题。 有人遇到过这种问题,或者我的代码在某些时候出错了吗?
答案 0 :(得分: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();
}
答案 1 :(得分:0)
。这就是我访问.net网络服务的方式。在我的情况下,我发送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();
}