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

我对Web要素服务(WFS)完全不熟悉,但我想在一个通过WFS发布数据的API之上构建一个带有ksoap2-android的Android应用程序。 我想请求API中的数据传递边界框参数以限制将返回的数据。

问题:

  • 如何将GetFeature对象放入SOAP信封中?
  • 如何在Android客户端上使用JAXBElement请参阅2012年3月15日的编辑

以下是API的一些链接,可能有助于了解其格式。

  • 获得性能
  • DescribeFeature

示例:WFS-1.1 GetFeature POST请求, http://data.wien.gv.at/daten/geoserver/wfs

      SHAPE  16.3739 48.2195 16.3759 48.2203      

这是我现在提出的Android代码。 这主要是来自ksoap2-android wiki的例子 。 我完全不确定 命名空间methodNameurl是否正确!

 // KSOAP2Client.java private class MyAsyncTask extends AsyncTask { 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; } } 

编辑: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 lowerCornerList = new Vector(); lowerCornerList.add(16.3739); lowerCornerList.add(48.2195); List upperCornerList = new Vector(); 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 propertyNames = new Vector(); propertyNames.add(new String("SHAPE")); PropertyNameType propertyNameType = new PropertyNameType(); propertyNameType.setContent(propertyNames); // TODO Check parameters of JAXBElement. JAXBElement e = new JAXBElement(null, null, envelopeType); BBOXType bboxType = new BBOXType(); bboxType.setPropertyName(propertyNameType); bboxType.setEnvelope(e); // TODO Check parameters of JAXBElement. JAXBElement spatialOps = new JAXBElement(null, null, bboxType); FilterType filterType = new FilterType(); filterType.setSpatialOps(spatialOps); QueryType queryType = new QueryType(); List typeNames = new Vector(); // 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); 

仍有一个重大问题,还有一些小问题。 主要问题是JAXBElement包含在Android拒绝使用的核心库javax.xml.bind.JAXBElement )中。 小问题在评论和TODO中说明。

编辑:2012年4月27日


当我读到这篇文章时,我想到类似的东西可能适用于我的问题。 我还没有尝试过。

编辑:2012年5月9日


当您尝试为Android编译JAXBElement时,以下是来自Eclipse的错误消息 。

@JJD我看到你在这里给我留了一个消息
我有一段时间的会议,但我看了你的问题,并很乐意尽可能多的帮助。 我发现你在阅读模式定义时遇到了问题。 你所拥有的这个定义是在另一个内部链接的,这就是为什么它让你感到困惑:

如果你继续: http : //schemas.opengis.net/wfs/1.1.0/wfs.xsd

采用方法“GetCapabilities”,让我们在Web服务定义中读取它:

PS:我没有测试这些但是:

  • 命名空间我认为应该是: http : //www.opengis.net/wfs (来自targetNamespace)
  • methodName: GetCapabilities
  • url: http : //schemas.opengis.net/wfs/1.1.0/wfs.xsd

现在您有了GetCapabilities的请求:

            

GetCapabilities有一个复杂类型的类型:GetCapabilitiesType,你可以在这个页面的一个xsd链接中找到它,恰好是“owsGetCapabilities.xsd”

– >打开后,即: http : //schemas.opengis.net/ows/1.0.0/owsGetCapabilities.xsd

您会发现这种复杂的类型定义:

    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.      When omitted, server shall return latest supported version.       When omitted or not supported by server, server shall return complete service metadata (Capabilities) document.       When omitted or not supported by server, server shall return service metadata document using the MIME type "text/xml".        When omitted or not supported by server, server shall return latest complete service metadata document.     

现在这个GetCapabilitiesType有元素/属性:
name =“AcceptVersions”的类型=“ows:AcceptVersionsType”和minOccurs =“0”即ie可以是null name =“Sections”类型=“ows:SectionsType”和minOccurs =“0”即ie可以是null name =“AcceptFormats “of type =”ows:AcceptFormatsType“和minOccurs =”0“ie可以是null name =”updateSequence“type =”ows:UpdateSequenceType“,它是可选的 – > use =”optional“

哪里可以找到这些属性定义?
– >在同一页面上你有: AcceptVersionsType是:

    Prioritized sequence of one or more specification versions accepted by client, with preferred versions listed first. See Version negotiation subclause for more information.       

所以AcceptVersionsType有一个类型的元素:VersionType可以在xsd owsOperationsMetadata.xsd(在这个页面的同一个链接上)找到,在它上面你有xsd:owsCommon.xsd这就是找到VersionType的地方,即 : http:/ /schemas.opengis.net/ows/1.0.0/owsCommon.xsd

   Specification version for OWS operation. The string value shall contain one xyz "version" value (eg, "2.1.3"). A version number shall contain three non-negative integers separated by decimal points, in the form "xyz". 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.     

和sectionsType是:

    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.       

(SectionsType具有简单类型String的元素)

而AcceptFormatsType是:

    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.       

(AcceptFormatsType有一个类型为MimeType的元素,与VersionType相同,即: http : //schemas.opengis.net/ows/1.0.0/owsCommon.xsd

   XML encoded identifier of a standard MIME type, possibly a parameterized MIME type.       

和UpdateSequenceType是(它是一个简单类型而不是复杂类型):

     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.     

(UpdateSequenceType是一个简单的类型)

现在我希望它更清楚如何阅读架构。 现在复杂类型意味着对象不同于简单类型(ex:int)。 如果您有复杂类型,并且使用的是ksoap2,则必须在实现kvmSerializable(ksoap2序列化接口)的类(对象)中创建本地表示。

现在,您可以阅读我的答案,了解如何执行此操作: Link1 , link2 , link3 。 我写了一些细节,可以帮助您了解如何开始编码。

这一天我不会在我的电脑上。 希望这会有所帮助,让我知道如果我说的任何东西都是暧昧的。

通常,从我对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()。