使用KSOAP Android创建SOAP请求
我需要生成一个像这样的soap请求 SOAP-REQUEST使用KSOAP Android创建SOAP请求,android,soap,ksoap2,Android,Soap,Ksoap2,我需要生成一个像这样的soap请求 SOAP-REQUEST POST /TennisMasters/TennisMasters.Listener.asmx HTTP/1.1 Host: playinkstudio.com Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://playinktennismasters.com/authenticateUser" <?xml
POST /TennisMasters/TennisMasters.Listener.asmx HTTP/1.1
Host: playinkstudio.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://playinktennismasters.com/authenticateUser"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<authenticateUser xmlns="http://playinktennismasters.com/">
<user>string</user>
</authenticateUser>
</soap:Body>
</soap:Envelope>
这是我从调试中得到的请求
<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://www.w3.org/2003/05/soap-encoding" xmlns:v="http://www.w3.org/2003/05/soap-envelope">
<v:Header />
<v:Body>
<authenticateUser xmlns="http://playinktennismasters.com/" **id="o0" c:root="1"**>
<user **i:type="d:string"**>{"email":"asad.mahmood@gmail.com","UserDate":"Feb 22, 2012 7:01:24 PM","GearId":0,"GearValue":0,"Income":0,"Level":0,"MatchResult":0,"MatchType":0,"OfferId":0,"OpponentId":0,"Partners":0,"ExhibitionCount":0,"PowerRuns":0,"PowerServes":0,"PowerShots":0,"Seeds":0,"Energy":0,"Cash":0,"Stamina":0,"Strength":0,"SubLevel":0,"TotalEnergy":0,"TotalStamina":0,"TrainingId":0,"Agility":0,"UserId":0,"Age":0,"ActivityId":0,"gearIsGift":0}</user>
</authenticateUser>
</v:Body>
</v:Envelope>
{“电子邮件”:“尽快”。mahmood@gmail.com“,”UserDate“:”2012年2月22日7:01:24 PM“,”GearId“:0,“GearValue“:0,“收入“:0”,“等级“:0”,“匹配结果“:0”,“匹配类型“:0”,“OfferId“:0”,“对手“:0”,“合作伙伴“:0”,“展示帐户“:0”,“PowerRuns“:0”,“PowerServes“:0”,“PowerShots“:0”,“种子“,”能量“:0”,“现金“,”耐力“,”强度“:0”,“次等级“:0”,“总能量“:0”,“总耐力”:0,“培训ID”:0,“敏捷性”:0,“用户ID”:0,“年龄”:0,“活动ID”:0,“gearIsGift”:0}
我不知道为什么authenticateUser中添加了额外的属性,如“id”和“c:root”。
和i:type=“d:String”中的额外属性。
请找人给我一个好的例子或教程,可以指导我创建像上面这样的请求,真的需要帮助,谢谢。我使用了simple HttpClient和Httppost, 请求信封的简单字符串
String temp = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<soap:Body>"
+ "<authenticateUser xmlns=\"http://playinktennismasters.com/\">"
+ "<user>%s</user>" + "</authenticateUser>" + "</soap:Body>"
+ "</soap:Envelope>";
ENVELOPE = String.format(temp, user);
String temp=“”
+ ""
+ ""
+ ""
+“%s”+”“+”
+ "";
信封=String.format(临时、用户);
现在使用的方法将创建post请求剩余参数并返回响应字符串
public String CallWebService(String url, String soapAction, String envelope) {
final DefaultHttpClient httpClient = new DefaultHttpClient();
// request parameters
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 15000);
// set parameter
HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);
// POST the envelope
HttpPost httppost = new HttpPost(url);
// add headers
httppost.setHeader("soapaction", soapAction);
httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
String responseString = "Nothingggg";
try {
// the entity holds the request
HttpEntity entity = new StringEntity(envelope);
httppost.setEntity(entity);
// Response handler
ResponseHandler<String> rh = new ResponseHandler<String>() {
// invoked when client receives response
public String handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
// get response entity
HttpEntity entity = response.getEntity();
// read the response as byte array
StringBuffer out = new StringBuffer();
byte[] b = EntityUtils.toByteArray(entity);
// write the response byte array to a string buffer
out.append(new String(b, 0, b.length));
return out.toString();
}
};
responseString = httpClient.execute(httppost, rh);
} catch (Exception e) {
e.printStackTrace();
Log.d("me","Exc : "+ e.toString());
}
// close the connection
httpClient.getConnectionManager().shutdown();
return responseString;
}
公共字符串CallWebService(字符串url、字符串soapAction、字符串信封){
final DefaultHttpClient httpClient=新的DefaultHttpClient();
//请求参数
HttpParams params=httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(参数,10000);
HttpConnectionParams.setSoTimeout(参数,15000);
//设置参数
HttpProtocolParams.setUseExpectContinue(httpClient.getParams(),true);
//把信封寄出去
HttpPost HttpPost=新的HttpPost(url);
//添加标题
setHeader(“soapaction”,soapaction);
setHeader(“内容类型”,“text/xml;charset=utf-8”);
字符串responseString=“nothinggg”;
试一试{
//实体持有请求
HttpEntity实体=新的StringEntity(信封);
httppost.setEntity(实体);
//响应处理程序
ResponseHandler rh=新ResponseHandler(){
//当客户端收到响应时调用
公共字符串句柄响应(HttpResponse响应)
抛出ClientProtocolException,IOException{
//获取响应实体
HttpEntity=response.getEntity();
//将响应读取为字节数组
StringBuffer out=新的StringBuffer();
字节[]b=EntityUtils.toByteArray(实体);
//将响应字节数组写入字符串缓冲区
append(新字符串(b,0,b.length));
return out.toString();
}
};
responseString=httpClient.execute(httppost,右侧);
}捕获(例外e){
e、 printStackTrace();
Log.d(“me”,“Exc:+e.toString());
}
//关闭连接
httpClient.getConnectionManager().shutdown();
回报率;
}
终于让它与KSOAP一起工作了。这是我使用的代码,也许它会帮助别人
final SoapObject request = new SoapObject(AppConsts.NAMESPACE,
usecaseString);
request.addProperty(addPropertyString, propertyJsonString);
final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
final HttpTransportSE androidHttpTransport = new HttpTransportSE(
AppConsts.URL);
androidHttpTransport.debug = true;
String soapAction = AppConsts.NAMESPACE + usecaseString;
try {
androidHttpTransport.call(soapAction, envelope);
SoapPrimitive resultSoapPrimitive;
resultSoapPrimitive = (SoapPrimitive) envelope.getResponse();
if (resultSoapPrimitive != null) {
result = resultSoapPrimitive.toString();
if (AppConsts.ENABLE_LOG)
Log.d(AppConsts.GAME_TITLE, "result json : " + result);
} else {
if (AppConsts.ENABLE_LOG)
Log.d(AppConsts.GAME_TITLE, "result json is NULL!!! ");
}
} catch (Exception e) {
e.printStackTrace();
Log.e("static", "Exception in making call to server");
}
为了创建上述请求,我们需要将这三个参数传递到代码中
AppConsts.NAMESPACE = "http://playinktennismasters.com"
usecaseString = "authenticateUser"
addPropertyString = "user"
要删除id e c:root属性,请将装饰设置为false:
envelope.setAddAdornments(false);
要删除i:type属性,对于simpleType,将隐式类型设置为true
envelope.implicitTypes = true;
但是在处理ComplexTypes时,要删除“i:type”,您需要KSOAP3.0.0RC1或更高版本。我现在使用的是3.0.0 RC2,但我会升级到稳定的3.0.0发行版,当它可用时。我假设您显示的第一个请求是从soapUI获得的?您必须知道ksoap2的envelope看起来与soap发送的有点不同,比如说在netbeans中使用常规soap完成的java程序。这不是问题,两者都会得到相同的结果,只是xml。您的请求是否有错误,或者您只是担心额外添加的属性?您好,请在此聊天室与我联系。我有一些疑问,这就是为什么,wt是您的url?a.svc或.asmx?我得到了一个a:InternalServiceFaults,
如何更改为
?您做了哪些更改?我不记得确切的行,但您可以浏览工作代码,它对我来说很好。并再次检查您的名称空间和usecaseString@waqas716:你能告诉我如何处理或解析复杂的SoapObject吗?3年后,你救了我一天!谢谢!
envelope.implicitTypes = true;