如何在 Android 中通过 POST 请求查询 Web 服务? [英] How to query a web service via POST request in Android?

查看:33
本文介绍了如何在 Android 中通过 POST 请求查询 Web 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Web Feature Service (WFS) 完全陌生,但我想构建一个在通过 WFS 发布其数据的 API 之上带有 ksoap2-android 的 Android 应用程序.我想从 API 请求数据,向它们传递边界框参数以限制将返回的数据.

I am totally new to Web Feature Service (WFS) but I want to build an Android application with ksoap2-android on top of an API publishing its data via WFS. I would like to request data from the API passing them the bounding box parameter to limit the data that will be returned.

问题:

  • 如何将 GetFeature 对象放入 SOAP 信封中?
  • 如何在 Android 客户端上使用 JAXBElement?查看 2012 年 3 月 15 日的编辑
  • How can I put the GetFeature object into the SOAP envelope?
  • How can I use JAXBElement on the Android client? See edit from March 15, 2012

这里有一些 API 链接,可能有助于理解它们的格式.

Here are some links to the API that might help to understand their format.

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

Example: WFS-1.1 GetFeature POST request, http://data.wien.gv.at/daten/geoserver/wfs

<?xml version="1.0" encoding="UTF-8"?>
<wfs:GetFeature service="WFS" version="1.1.0"
  outputFormat="JSON"
  xmlns:ogdwien="http://www.wien.gv.at/ogdwien"
  xmlns:wfs="http://www.opengis.net/wfs"
  xmlns:ogc="http://www.opengis.net/ogc"
  xmlns:gml="http://www.opengis.net/gml"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.opengis.net/wfs
  http://schemas.opengis.net/wfs/1.1.0/wfs.xsd" >
 <wfs:Query typeName="ogdwien:BAUMOGD">
   <ogc:Filter>
   <ogc:BBOX>
   <ogc:PropertyName>SHAPE</ogc:PropertyName>
   <gml:Envelope srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
     <gml:lowerCorner>16.3739 48.2195</gml:lowerCorner>
     <gml:upperCorner>16.3759 48.2203</gml:upperCorner>
   </gml:Envelope>
   </ogc:BBOX>
   </ogc:Filter>
  </wfs:Query>
</wfs:GetFeature>

这是我现在想出的 Android 代码.这主要受到 来自 ksoap2-android wiki 的示例的启发.我完全不确定namespacemethodNameurl 是否正确!

This is the Android code I came up with by now. This is mostly inspirated by examples from the ksoap2-android wiki. I am totally unsure whether the namespace, methodName and url are correct!

// 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;
    }
}

2012 年 3 月 15 日

我设法走得更远,几乎达到了解决方案.我找到了 XML 请求中使用的命名空间的 schema 定义 并链接他们到我的项目.这允许我为请求组合对象.

I managed to get further and I almost reached what seems to be the solution. I found the schema definitions for those namespaces used in the XML request and linked them to my project. That allows me to assemble the objects for the request.

// 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);

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

Still there are a major problem and some minor problems left. The major problem is that JAXBElement is contained in a core library (javax.xml.bind.JAXBElement) that Android refuses to consume. The minor problems are stated in the comments and TODOs.

2012 年 4 月 27 日

当我阅读这篇文章时,我想类似的事情可能适用于我的问题.我还没试过.

As I read this post I imagine that something similar might apply to my problem. I haven't tried yet.

2012 年 5 月 9 日

这是当您尝试为 Android 编译 JAXBElement 时来自 Eclipse 的错误消息.

Here is the error message from Eclipse when you try to compile JAXBElement for Android.

推荐答案

@JJD 我看到你在 这里
我一会儿要开会,但我看了你的问题,很乐意提供尽可能多的帮助.我发现您在阅读架构定义时遇到问题.你的这个 ws 定义被链接在另一个里面,这就是为什么它让你读起来很困惑:

@JJD i see you left me a msg in here
I have a meeting in a while, but i took a look at your question and would be glad to help as much as possible. I see you have a problem reading the schema definition . this ws definition you have is chained one inside the other thats why it confused you reading it:

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

获取方法GetCapabilities",让我们在 Web 服务定义中读取它:

take the method "GetCapabilities" and let's read it in the Web service definition:

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

PS:I did not test these but:

  • namespace i think should be : http://www.opengis.net/wfs (from targetNamespace)
  • methodName: GetCapabilities
  • url: http://schemas.opengis.net/wfs/1.1.0/wfs.xsd


现在您有 GetCapabilities 的请求:


Now You have the request of GetCapabilities:

<!-- 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>

GetCapabilities 有一个复杂的类型:GetCapabilitiesType,您可以在本页的一个 xsd 链接中找到它,准确地说是 "owsGetCapabilities.xsd"

GetCapabilities has a complex type of the type: GetCapabilitiesType, which you find in one of the xsd link on this page, precisely "owsGetCapabilities.xsd"

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

你会发现这个复杂的类型定义:

You find this complex type definition:

<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>

现在这个 GetCapabilitiesType 有元素/属性:
name="AcceptVersions" of type="ows:AcceptVersionsType" 和 minOccurs="0" 即可以为空name="Sections" of type="ows:SectionsType" 和 minOccurs="0" 即可以为空name="AcceptFormats" of type="ows:AcceptFormatsType" 和 minOccurs="0" 即可以为空name="updateSequence" of type="ows:UpdateSequenceType" 并且它是可选的 -->use="optional"

Now this GetCapabilitiesType has elements /attributes:
name="AcceptVersions" of type="ows:AcceptVersionsType" and minOccurs="0" ie can be null name="Sections" of type="ows:SectionsType" and minOccurs="0" ie can be null name="AcceptFormats" of type="ows:AcceptFormatsType" and minOccurs="0" ie can be null name="updateSequence" of type="ows:UpdateSequenceType" and its is optional -->use="optional"

在哪里可以找到这些属性定义?
-->在同一页面上,您有:接受版本类型是:

Where to find these attributes definitions?
-->on this same page you have: AcceptVersionsType is :

<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>


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

<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>

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

(SectionsType has an element of simple type String)

而 AcceptFormatsType 是:

And AcceptFormatsType is:

<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>

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

(AcceptFormatsType has an element of type MimeType which is found same place as VersionType ie : http://schemas.opengis.net/ows/1.0.0/owsCommon.xsd

<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>

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

and UpdateSequenceType is( it is a simple type not complex type):

 <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>

(UpdateSequenceType 是一个简单类型)

(UpdateSequenceType is a simple type)

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

Now i hope it got clearer how to read the schema . Now complex type means object unlike simple type (ex: int). When you have complex types, and you are using ksoap2, you have to create local representations in classes (objects) that implements kvmSerializable ( a ksoap2 serialization interface).

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

Now , you can read my answers to know how to do that on : Link1,link2,link3. I wrote some details which will help you understand how to start coding.

我今天不会在我的电脑上.希望这会有所帮助,如果我说的任何内容有歧义,请告诉我.

I won't be on my pc for the day. Hope this helps,let me know if anything of wt i said is ambigous.

这篇关于如何在 Android 中通过 POST 请求查询 Web 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆