Java 如何在Android中通过POST请求查询web服务?

Java 如何在Android中通过POST请求查询web服务?,java,android,web-services,jaxb,ksoap2,Java,Android,Web Services,Jaxb,Ksoap2,我是个新手,但我想在API的基础上构建一个Android应用程序,通过WFS发布数据。我希望从API请求数据,并向它们传递边界框参数,以限制将返回的数据 问题: 如何将GetFeature对象放入SOAP信封中 如何在Android客户端上使用JAXBElement?参见2012年3月15日起编辑 下面是一些指向API的链接,可能有助于理解它们的格式 示例:WFS-1.1 GetFeature POST请求 形状 16.3739 48.2195 16.3759 48.2203 这

我是个新手,但我想在API的基础上构建一个Android应用程序,通过WFS发布数据。我希望从API请求数据,并向它们传递边界框参数,以限制将返回的数据

问题:

  • 如何将
    GetFeature
    对象放入SOAP信封中
  • 如何在Android客户端上使用
    JAXBElement
    ?参见2012年3月15日起编辑
下面是一些指向API的链接,可能有助于理解它们的格式

示例:WFS-1.1 GetFeature POST请求


形状
16.3739 48.2195
16.3759 48.2203
这是我现在想出的Android代码。这主要是由以下因素激发的。我完全不确定名称空间、方法名和url是否正确

// KSOAP2Client.java

private class MyAsyncTask extends AsyncTask<Void, Void, Object> {
    String namespace = "http://www.wien.gv.at/ogdwien";
    String methodName = "GetFeature";
    String url = "http://data.wien.gv.at/daten/geoserver/wfs";

    protected Object doInBackground(Void... voids) {
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;
        SoapObject soapObject = new SoapObject(namespace, methodName);
        envelope.setOutputSoapObject(soapObject);

        // TODO Put request parameters in the envelope. But how?

        try {
            HttpTransportSE httpTransportSE = new HttpTransportSE(url);
            httpTransportSE.debug = true;
            httpTransportSE.call(namespace + methodName, envelope);
            return (Object)soapSerializationEnvelope.getResponse();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return null;
    }
}
//KSOAP2Client.java
私有类MyAsyncTask扩展了AsyncTask{
字符串命名空间=”http://www.wien.gv.at/ogdwien";
String methodName=“GetFeature”;
字符串url=”http://data.wien.gv.at/daten/geoserver/wfs";
受保护对象doInBackground(无效…无效){
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=false;
SoapObject SoapObject=新的SoapObject(命名空间、方法名);
envelope.setOutputSoapObject(soapObject);
//TODO将请求参数放入信封中。但是如何?
试一试{
HttpTransportSE HttpTransportSE=新的HttpTransportSE(url);
httpTransportSE.debug=true;
调用(命名空间+方法名,信封);
返回(对象)soapSerializationEnvelope.getResponse();
}捕获(异常){
异常。printStackTrace();
}
返回null;
}
}
编辑:2012年3月15日


我设法走得更远,几乎找到了解决办法。我找到了XML请求中使用的名称空间的名称,并将它们链接到我的项目。这允许我为请求组装对象

// TODO The core libraries won't work with Android.
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;

// TODO Not sure if the versions fit with the service.
import net.opengis.filter.v_1_1_0.BBOXType;
import net.opengis.filter.v_1_1_0.FilterType;
import net.opengis.filter.v_1_1_0.PropertyNameType;
import net.opengis.gml.v_3_1_1.DirectPositionType;
import net.opengis.gml.v_3_1_1.EnvelopeType;
import net.opengis.wfs.v_1_1_0.GetFeatureType;
import net.opengis.wfs.v_1_1_0.QueryType;

[...]

List<Double> lowerCornerList = new Vector<Double>();
lowerCornerList.add(16.3739);
lowerCornerList.add(48.2195);

List<Double> upperCornerList = new Vector<Double>();
upperCornerList.add(16.3759);
upperCornerList.add(48.2203);

DirectPositionType lowerCornerDirectPositionType = new DirectPositionType();
lowerCornerDirectPositionType.setValue(lowerCornerList);
DirectPositionType upperCornerDirectPositionType = new DirectPositionType();
upperCornerDirectPositionType.setValue(upperCornerList);

EnvelopeType envelopeType = new EnvelopeType();
envelopeType.setSrsName("http://www.opengis.net/gml/srs/epsg.xml#4326");
envelopeType.setLowerCorner(lowerCornerDirectPositionType);
envelopeType.setUpperCorner(upperCornerDirectPositionType);

List<Object> propertyNames = new Vector<Object>();
propertyNames.add(new String("SHAPE"));

PropertyNameType propertyNameType = new PropertyNameType();
propertyNameType.setContent(propertyNames);

// TODO Check parameters of JAXBElement.
JAXBElement<EnvelopeType> e = new JAXBElement<EnvelopeType>(null, null, envelopeType);
BBOXType bboxType = new BBOXType();
bboxType.setPropertyName(propertyNameType);
bboxType.setEnvelope(e);

// TODO Check parameters of JAXBElement.
JAXBElement<BBOXType> spatialOps = new JAXBElement<BBOXType>(null, null, bboxType);
FilterType filterType = new FilterType();
filterType.setSpatialOps(spatialOps);

QueryType queryType = new QueryType();
List<QName> typeNames = new Vector<QName>();
// TODO Check parameters of QName.
typeNames.add(new QName("ogdwien", "BAUMOGD"));
queryType.setTypeName(typeNames);

GetFeatureType featureType = new GetFeatureType();
featureType.setService("WFS");
featureType.setVersion("1.1.0");
featureType.setOutputFormat("JSON");
featureType.setMaxFeatures(new BigInteger("5"));

String namespace = "http://www.wien.gv.at/ogdwien";
String methodName = "GetFeature";
// TODO Is this the correct action?
String action = "http://data.wien.gv.at/daten/wfs?service=WFS&request=GetFeature&version=1.1.0&typeName=ogdwien:BAUMOGD&srsName=EPSG:4326";
String url = "http://data.wien.gv.at/daten/geoserver/wfs";

// TODO Is this the correct way to add GetFeature?
SoapObject soapObject = new SoapObject(namespace, methodName);
PropertyInfo propertyInfo = new PropertyInfo();
propertyInfo.setName("GetFeature");
propertyInfo.setValue(featureType);
soapObject.addProperty(propertyInfo);
//核心库的TODO不能与Android一起使用。
导入javax.xml.bind.JAXBElement;
导入javax.xml.namespace.QName;
//TODO不确定这些版本是否适合该服务。
导入net.opengis.filter.v_1_1_0.BBOXType;
导入net.opengis.filter.v_1_1_0.FilterType;
导入net.opengis.filter.v_1_1_0.PropertyNameType;
导入net.opengis.gml.v_3_1_1.DirectPositionType;
导入net.opengis.gml.v_3_1_1.EnvelopeType;
导入net.opengis.wfs.v_1_1_0.GetFeatureType;
导入net.opengis.wfs.v_1_1_0.QueryType;
[...]
List lowerCornerList=新向量();
加上(16.3739);
下角列表添加(48.2195);
List upperCornerList=新向量();
上角点列表。添加(16.3759);
上角点列表。添加(48.2203);
DirectPositionType lowerCornerDirectPositionType=新的DirectPositionType();
lowerCornerDirectPositionType.setValue(lowerCornerList);
DirectPositionType upperCornerDirectPositionType=新的DirectPositionType();
upperCornerDirectPositionType.setValue(upperCornerList);
EnvelopeType EnvelopeType=新的EnvelopeType();
envelopeType.setSrsName(“http://www.opengis.net/gml/srs/epsg.xml#4326");
envelopeType.setLowerCorner(lowerCornerDirectPositionType);
envelopeType.setUpperCorner(upperCornerDirectPositionType);
列表属性名称=新向量();
添加(新字符串(“形状”);
PropertyNameType PropertyNameType=新的PropertyNameType();
setContent(propertyNames);
//TODO检查JAXBElement的参数。
JAXBElement e=新的JAXBElement(null,null,envelopeType);
BBOXType BBOXType=新的BBOXType();
setPropertyName(propertyNameType);
bboxType.setEnvelope(e);
//TODO检查JAXBElement的参数。
JAXBElement spatialOps=新的JAXBElement(null,null,bboxType);
FilterType FilterType=新的FilterType();
filterType.setSpatialOps(spatialOps);
QueryType QueryType=新的QueryType();
列表类型名称=新向量();
//TODO检查QName的参数。
添加(新的QName(“ogdwien”、“BAUMOGD”);
queryType.setTypeName(类型名称);
GetFeatureType featureType=新的GetFeatureType();
featureType.setService(“WFS”);
featureType.setVersion(“1.1.0”);
setOutputFormat(“JSON”);
setMaxFeatures(新的BigInteger(“5”));
字符串命名空间=”http://www.wien.gv.at/ogdwien";
String methodName=“GetFeature”;
//TODO这是正确的操作吗?
字符串操作=”http://data.wien.gv.at/daten/wfs?service=WFS&request=GetFeature&version=1.1.0&typeName=ogdwien:BAUMOGD&srsName=EPSG:4326";
字符串url=”http://data.wien.gv.at/daten/geoserver/wfs";
//TODO这是添加GetFeature的正确方法吗?
SoapObject SoapObject=新的SoapObject(命名空间、方法名);
PropertyInfo PropertyInfo=新的PropertyInfo();
propertyInfo.setName(“GetFeature”);
propertyInfo.setValue(特性类型);
addProperty(propertyInfo);
还有一个大问题和一些小问题。主要问题是
JAXBElement
包含在Android拒绝使用的核心库中(
javax.xml.bind.JAXBElement
)。评论和待办事项中说明了次要问题

编辑:2012年4月27日


当我读到这篇文章时,我想类似的东西可能会应用到我的问题上。我还没试过

编辑:2012年5月9日



以下是您尝试为Android编译JAXBElement时遇到的问题。

通常,根据我对ksoap2的一点经验,您会这样做

SoapObject request = new SoapObject("http://www.webserviceX.NET", "GetCitiesByCountry");
String soapAction = "http://www.webserviceX.NET/GetCitiesByCountry";

request.addProperty("CountryName", "india");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
envelope.dotNet = true;

HttpTransport ht = new HttpTransport("http://www.webservicex.net/globalweather.asmx");
ht.debug = true;
//System.err.println( ht.requestDump );

ht.call(soapAction,envelope);
System.out.println("####################: " +envelope.getResponse());
//SoapObject result = (SoapObject)envelope.getResponse();

因此,基本上你应该只使用你的soapObject并调用addProperty()。

@JJD我看到你给我留下了一条消息
我有一段时间要开会,但我看了你的问题,很乐意尽可能多地帮助你。我发现您在阅读模式定义时遇到问题。这
SoapObject request = new SoapObject("http://www.webserviceX.NET", "GetCitiesByCountry");
String soapAction = "http://www.webserviceX.NET/GetCitiesByCountry";

request.addProperty("CountryName", "india");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
envelope.dotNet = true;

HttpTransport ht = new HttpTransport("http://www.webservicex.net/globalweather.asmx");
ht.debug = true;
//System.err.println( ht.requestDump );

ht.call(soapAction,envelope);
System.out.println("####################: " +envelope.getResponse());
//SoapObject result = (SoapObject)envelope.getResponse();
<!-- REQUEST -->
<xsd:element name="GetCapabilities" type="wfs:GetCapabilitiesType"/>
<xsd:complexType name="GetCapabilitiesType">
    <xsd:annotation>
    </xsd:annotation>
    <xsd:complexContent>
    <xsd:extension base="ows:GetCapabilitiesType">
    <xsd:attribute name="service" type="ows:ServiceType" use="optional" default="WFS"/>
    </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>
<complexType name="GetCapabilitiesType">
<annotation>
     <documentation>
         XML encoded GetCapabilities operation request. This operation 
         allows clients to retrieve service metadata about a specific service instance.
         In this XML encoding, no "request" parameter is included, since the element name 
         specifies the specific operation. This base type shall be extended by each specific 
         OWS to include the additional required "service" attribute, with the correct value for that OWS. 
     </documentation>
</annotation>
<sequence>
     <element name="AcceptVersions" type="ows:AcceptVersionsType" minOccurs="0">
         <annotation>
             <documentation>When omitted, server shall return latest supported version. 
             </documentation>
         </annotation>
     </element>
     <element name="Sections" type="ows:SectionsType" minOccurs="0">
         <annotation>
             <documentation>
                 When omitted or not supported by server, 
                 server shall return complete service metadata (Capabilities) document. 
             </documentation>
         </annotation>
     </element>
     <element name="AcceptFormats" type="ows:AcceptFormatsType" minOccurs="0">
         <annotation>
             <documentation>
                 When omitted or not supported by server, server shall return service metadata 
                 document using the MIME type "text/xml". 
             </documentation>
         </annotation>
     </element>
 </sequence>
<attribute name="updateSequence" type="ows:UpdateSequenceType" use="optional">
     <annotation>
         <documentation>
             When omitted or not supported by server, 
             server shall return latest complete service 
             metadata document. 
         </documentation>
     </annotation>
</attribute>
</complexType>
<complexType name="AcceptVersionsType">
 <annotation>
 <documentation>
  Prioritized sequence of one or more specification versions accepted by client, with preferred versions listed first. See Version negotiation subclause for more information.     
 </documentation>
 </annotation>
 <sequence>
  <element name="Version" type="ows:VersionType" maxOccurs="unbounded"/>
 </sequence>
</complexType>
<simpleType name="VersionType">
  <annotation>
    <documentation>Specification version for OWS operation. The string value shall contain one x.y.z "version" value (e.g., "2.1.3"). A version number shall contain three non-negative integers separated by decimal points, in the form "x.y.z". The integers y and z shall not exceed 99. Each version shall be for the Implementation Specification (document) and the associated XML Schemas to which requested operations will conform. An Implementation Specification version normally specifies XML Schemas against which an XML encoded operation response must conform and should be validated. See Version negotiation subclause for more information. </documentation>
  </annotation>
  <restriction base="string"/>
</simpleType>
<complexType name="SectionsType">
 <annotation>
   <documentation>
    Unordered list of zero or more names of requested sections in complete service metadata document. Each Section value shall contain an allowed section name as specified by each OWS specification. See Sections parameter subclause for more information.            
   </documentation>
 </annotation>
 <sequence>
    <element name="Section" type="string" minOccurs="0" maxOccurs="unbounded"/>
 </sequence>
</complexType>
<complexType name="AcceptFormatsType">
 <annotation>
   <documentation>
    Prioritized sequence of zero or more GetCapabilities operation response formats desired by client, with preferred formats listed first. Each response format shall be identified by its MIME type. See AcceptFormats parameter use subclause for more information. 
   </documentation>
 </annotation>
 <sequence>
  <element name="OutputFormat" type="ows:MimeType" minOccurs="0" maxOccurs="unbounded"/>
  </sequence>
</complexType>
<simpleType name="MimeType">
  <annotation>
    <documentation>XML encoded identifier of a standard MIME type, possibly a parameterized MIME type. </documentation>
  </annotation>
  <restriction base="string">
    <pattern value="(application|audio|image|text|video|message|multipart|model)/.+(;s*.+=.+)*"/>
  </restriction>
</simpleType>
 <simpleType name="UpdateSequenceType">
 <annotation>
  <documentation>
   Service metadata document version, having values that are "increased" whenever any change is made in service metadata document. Values are selected by each server, and are always opaque to clients. See updateSequence parameter use subclause for more information.     
   </documentation>
  </annotation>
 <restriction base="string"/>
 </simpleType>