gpt4 book ai didi

android - 适用于 Android 的 KSOAP2 提供

转载 作者:行者123 更新时间:2023-11-29 00:49:02 36 4
gpt4 key购买 nike

我正在尝试访问其 WSDL 位于 http://srilanka.lk:9080/services/CropServiceProxy?wsdl 的 Web 服务.使用 SoapUI 我发送了一个请求并成功地得到了响应。

然后我尝试使用适用于 Android 的 KSoap2 获得响应。但我得到的只是一个 SoapFault 错误。代码如下。

String NAMESPACE = "http://schemas.icta.lk/xsd/crop/handler/v1";

    String URL = "http://www.srilanka.lk:9080/services/CropServiceProxy.CropServiceProxyHttpSoap12Endpoint";

String method_name = "getCropDataList";
String SOAP_ACTION = method_name;

SoapObject request = new SoapObject(NAMESPACE, method_name);
String crop_code_str = String.valueOf(code.getText().toString());
System.out.println(crop_code_str);
System.out.println(request.toString());
request.addProperty("code", crop_code_str );
System.out.println(request.getProperty("code").toString());

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.setOutputSoapObject(request);

System.out.println("body out : " + envelope.bodyOut.toString());

HttpTransportSE http_transport = new HttpTransportSE(URL);
try {
http_transport.call(SOAP_ACTION, envelope);
System.out.println(envelope.bodyIn.toString());

} catch (Exception e) {
e.printStackTrace();
answer.setText("error caught");
}

结果如下。

W/System.err(  394): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://www.w3.org/2001/12/soap-envelope}Envelope (position:START_TAG <{http://schemas.xmlsoap.org/soap/envelope/}soapenv:Envelope>@1:114 in java.io.InputStreamReader@43e81258)  

我尝试使用 VER11,但它随后将以下错误作为 bodyIn。

I/System.out(  365): SoapFault - faultcode: 'soapenv:Server' faultstring: 'org.apache.axis2.databinding.ADBException: Unexpected subelement code' faultactor: 'null' detail: org.kxml2.kdom.Node@43e84648

Ksoap2 for android 有什么问题吗?那么有没有另一种方式可以在 Android 中使用 Web 服务? (只有 SOAP 可用)

更新:我尝试了复杂类型的 KvmSerializable 接口(interface)。以下是我的代码。我仍然遇到同样的错误。在 http_transport.call 行抛出 XMLPullParser 错误。

这是客户端的复杂类型实现。我只有 WSDL。

public class CropInfo implements KvmSerializable {

private String name;
private float price;
private String location;

@Override
public Object getProperty(int arg0) {
switch (arg0){
case 0:
return name;
case 1:
return price;
case 2:
return location;
default:
return null;
}
}

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

@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
switch (arg0){
case 0:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "Name";
break;
case 1:
arg2.type = Float.class;
arg2.name = "Price";
break;
case 2:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "Location";
break;
default:
break;
}

}

@Override
public void setProperty(int arg0, Object arg1) {
switch(arg0){
case 0:
name = arg1.toString();
break;
case 1:
price = Float.parseFloat(arg1.toString());
case 2:
location = arg1.toString();
default:
break;
}
}
}

访问和解析 Web 服务响应的修改代码。

String NAMESPACE = "http://schemas.icta.lk/xsd/crop/handler/v1/"; 字符串 URL = "http://www.srilanka.lk:9080/services/CropServiceProxy.CropServiceProxyHttpSoap12Endpoint"; String method_name = "getCropDataList"; 字符串 SOAP_ACTION = "http://schemas.icta.lk/xsd/crop/handler/v1/getCropDataList";

    SoapObject soap_request = new SoapObject(NAMESPACE, method_name);
String crop_code_str = String.valueOf(code.getText().toString());
System.out.println(crop_code_str);
System.out.println(soap_request.toString());
soap_request.addProperty("code", crop_code_str );
System.out.println(soap_request.getProperty("code").toString());

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.setOutputSoapObject(soap_request);
envelope.addMapping(NAMESPACE, "cropInfo", CropInfo.class);
//envelope.dotNet=true;

Marshal floatMarshal = new MarshalFloat();
floatMarshal.register(envelope);

System.out.println("body out : " + envelope.bodyOut.toString());

//AndroidHttpTransport http_transport = new AndroidHttpTransport(URL);
HttpTransportSE http_transport = new HttpTransportSE(URL);
try {
//error comes from this call
http_transport.call(SOAP_ACTION, envelope);


Vector<CropInfo> result_array = (Vector<CropInfo>)envelope.getResponse();
if(result_array != null){
for (CropInfo current_crop: result_array){
System.out.println(current_crop.getName());
System.out.println(Float.toString(current_crop.getPrice()));
}
}

} catch (Exception e) {
e.printStackTrace();
answer.setText("error caught");
}

最佳答案

我读过很多关于使用 KSoap 的痛苦和不一致的话题。您可以使用 DefaultHttpClient 类来执行 http 发布,只需手动制作 SOAP header 并解析响应。

关于android - 适用于 Android 的 KSOAP2 提供,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4557180/

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