gpt4 book ai didi

java - 我可以使用 KSoap2 将静态 XML 文件作为请求发送吗

转载 作者:行者123 更新时间:2023-11-30 11:00:39 25 4
gpt4 key购买 nike

我正在使用 KSoap2 尝试构建下面的 XML 文件请求。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:_5="www.444.com">

<soapenv:Header/>
<soapenv:Body>
<_5:GetStaff>
<!--Optional:-->
<_5:Request>
<!--Optional:-->
<_5:SourceCredentials>
<!--Optional:-->
<_5:SourceName>sourcename</_5:SourceName>
<!--Optional:-->
<_5:Password>password=</_5:Password>
<!--Optional:-->
<_5:SiteIDs>
<!--Zero or more repetitions:-->
<_5:int>1111</_5:int>
</_5:SiteIDs>
</_5:SourceCredentials>
</_5:Request>
</_5:GetStaff>
</soapenv:Body>
</soapenv:Envelope>

我试图采用它并生成以下代码,但似乎子元素给我带来了问题。我是 Ksoap2 的新手,找不到清晰的结构化解决方案。

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

request.addProperty("SourceName", "sourcename");
request.addProperty("Password", "password=");
request.addProperty("int", "1111");

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

HttpTransportSE htse = new HttpTransportSE(URL);

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

ret = resultString.toString();
}
catch(Exception e){
e.printStackTrace();
}

我将如何使用 KSoap2 构建树以便正确访问子元素。如果没有此结构,api 将无法正确响应。

有没有办法直接从 xml 资源中发送这个 xml 文件?

或者有别的办法吗?

更新:尝试为复杂元素创建类

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

import java.util.Hashtable;

/**
* Created by randypfohl on 7/15/15.
*/
public class SourceCredentials implements KvmSerializable {

//Xml variables
private String sourceName;
private String password;
private SiteIDs site;

public SourceCredentials() {
}

public void setSourceName(String sourceName) {
this.sourceName = sourceName;
}

public String getSourceName() {
return this.sourceName;
}

public void setPassword(String password) {
this.password = password;
}

public String getPassword() {
return this.password;
}

public void setSiteIDs(SiteIDs site) {
this.site = site;
}

@Override
public Object getProperty(int index) {

Object toreturn = null;

switch (index) {
case 0:
toreturn = this.sourceName;
case 1:
toreturn = this.password;
case 2:
toreturn = this.site;
default:
break;
}
return toreturn;
}

@Override
public int getPropertyCount () {
return 3;
}


@Override
public void setProperty ( int index, Object value){
switch (index) {
case 0:
this.sourceName = value.toString();
break;
case 1:
this.password = value.toString();
break;
case 2:
site.setProperty(0, value);
break;
}

}

@Override
public void getPropertyInfo ( int index, Hashtable hashtable, PropertyInfo propertyInfo){
switch (index) {
case 0:
propertyInfo.name = "SourceName";
propertyInfo.type = PropertyInfo.STRING_CLASS;
break;
case 1:
propertyInfo.name = "Password";
propertyInfo.type = PropertyInfo.STRING_CLASS;
break;
case 2:
propertyInfo.name = "SiteIDs";
propertyInfo.type = SiteIDs.class;
break;
default:
break;
}
}

@Override
public String getInnerText () {
return null;
}

@Override
public void setInnerText (String s){

}
}

这是 siteids 类。

import java.util.Hashtable;
import java.util.Vector;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

public class SiteIDs extends Vector<Integer> implements KvmSerializable {

@Override
public Object getProperty(int arg0) {
return this.get(arg0);
}

@Override
public String getInnerText () {
return null;
}

@Override
public void setInnerText (String s){

}

@Override
public int getPropertyCount() {
return 1;
}

@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
arg2.name = "int";
arg2.type = PropertyInfo.INTEGER_CLASS;
}

@Override
public void setProperty(int arg0, Object arg1) {
this.add(Integer.valueOf(arg1.toString()));
}
}

这是我创建的代码。

/* String ret ="失败";

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

SourceCredentials sourceCredentials = new SourceCredentials();
sourceCredentials.setProperty(0, "name");
sourceCredentials.setProperty(1, "password");

SiteIDs siteIDS = new SiteIDs();
siteIDS.setProperty(0, "1100");
sourceCredentials.setSiteIDs(siteIDS);

request.addProperty("SourceCredentials", sourceCredentials);

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

envelope.addMapping(NAMESPACE, SourceCredentials.class.getSimpleName(), SourceCredentials.class);
envelope.addMapping(NAMESPACE, SiteIDs.class.getSimpleName(), SiteIDs.class);

try{
HttpTransportSE htse = new HttpTransportSE(URL);
htse.debug = true;
htse.setXmlVersionTag("<!--?xml version=\"1.0\" encoding= \"UTF-8\" ?-->");
htse.call(SOAP_ACTION, envelope);
SoapPrimitive resultString = (SoapPrimitive) envelope.getResponse();

ret = resultString.toString();
System.out.println(ret);
}
catch(Exception e){
e.printStackTrace();
}

return ret;

我想不通。我似乎总是得到一个奇怪的错误。

 SoapFault - faultcode: 'soap:Server' faultstring: 'Server was unable to process request. ---> Object reference not set to an instance of an object.' faultactor: 'null' detail: org.kxml2.kdom.Node@b1eb5d78

最佳答案

您必须创建可序列化对象并将它们传递给 SOAP 对象。

public class SourceCredentials implements KvmSerializable {

//Xml variables
private String sourceName;
private String password;
private SiteIDs siteIDs;


public SourceCredentials(){
}

public setSourceName(String sourceName){
this.sourceName = sourceName;
}

public String getSourceName(){
return this.sourceName;
}

public setPassword(String password){
this.password = password;
}

public String getPassword(){
return this.password;
}

public setSiteIDs(SiteIDs siteIDs){
this.siteIDs = siteIDs;
}

public getSiteIDs(){
return this.siteIDs;
}

@Override
public Object getProperty(int index) {

switch(index){
case 0:
return this.sourceName;
case 1:
return this.password;
case 2:
return this.siteIDs;
default:
break
}

@Override
public int getPropertyCount() {
return 3;


@Override
public void setProperty(int index, Object value) {
switch(index){
case 0:
this.sourceName = value.toString();
break;
case 1:
this.password = value.toString();
break;
case 2:
this.siteIDs = (SiteIDs) value;
break;
}

}

@Override
public void getPropertyInfo(int index, Hashtable hashtable, PropertyInfo propertyInfo) {
switch (index){
case 0:
propertyInfo.name = "SourceName";
propertyInfo.type = PropertyInfo.STRING_CLASS;
break;
case 1:
propertyInfo.name = "Password";
propertyInfo.type = PropertyInfo.STRING_CLASS;
break;
case 2:
propertyInfo.name = "SiteIDs";
propertyInfo.type = PropertyInfo.OBJECT_CLASS;
break;
default:
break;
}
}

@Override
public String getInnerText() {
return null;
}

@Override
public void setInnerText(String s) {

}

并且您对 SiteId 执行相同的操作

然后传递 Soap 对象。

编辑

你试试这个?

SourceCredentials sourceCredentials = new SourceCredentials();

//Create SiteIDs KvmSerializable object
SiteIDs siteIDS = new SiteIDS();
siteIDS.setInt(1111)

//Setters
sourceCredentials.setSourceName("sourcename");
sourceCredentials.setPassword("password=");
sourceCredentials.setSiteIDS(siteIDS);


request.addProperty("SourceCredentials", sourceCredentials);

编辑 2

试试这个

SiteIDS.java

public class SiteIDs implements KvmSerializable {

private List<Integer> Int; //change name of variable, int it's a reserved name

public SiteIDs() {
}

public List<Integer> getInt() {
return Int;
}

public void setInt(List<Integer> anInt) {
Int = anInt;
}

@Override
public Object getProperty(int i) {
switch (i){
case 0:
return Int;
default:
break;
}
return null;
}

@Override
public int getPropertyCount() {
return 1;
}

@Override
public void setProperty(int i, Object value) {
switch (i) {
case 0:
this.Int = (List<Integer>) value;
break;
default:
break;
}
}

@Override
public void getPropertyInfo(int i, Hashtable hashtable, PropertyInfo propertyInfo) {
switch (i) {
case 0:
propertyInfo.name = "int";
propertyInfo.type = List.class;
break;
default:
break;
}
}

@Override
public String getInnerText() {
return null;
}

@Override
public void setInnerText(String s) {

}

还有这个:

SourceCredentials sourceCredentials = new SourceCredentials();

//Create SiteIDs KvmSerializable object
SiteIDs siteIDS = new SiteIDS();
List<Integer> ints = new ArrayList<Integer>();
ints.add(1111); //add one o more ints
siteIDS.setInts(ints);
//Setters
sourceCredentials.setSourceName("sourcename");
sourceCredentials.setPassword("password=");
sourceCredentials.setSiteIDS(siteIDS);


request.addProperty("SourceCredentials", sourceCredentials);

关于java - 我可以使用 KSoap2 将静态 XML 文件作为请求发送吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31438114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com