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

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

问题描述

我完全陌生网络要素服务(WFS),但我想建立一个Android应用程序在一个API,通过世界粮食首脑会议发布的数据之上 ksoap2,机器人 。我想从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.

问题:

  • 我怎样才能把获得要素对象到SOAP信封?
  • 如何使用的JAXBElement Android客户端吗? 从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.

  • <一个href="http://data.wien.gv.at/daten/wfs?service=WFS&request=GetCapabilities&version=1.1.0&typeName=ogdwien%3aBAUMOGD&srsName=EPSG:4326"相对=nofollow>获得性能
  • <一个href="http://data.wien.gv.at/daten/wfs?service=WFS&request=DescribeFeatureType&version=1.1.0&typeName=ogdwien%3aBAUMOGD&srsName=EPSG:4326&TYPENAME=ogdwien%3aBAUMOGD"相对=nofollow> DescribeFeature
  • GetCapabilities
  • DescribeFeature

例:WFS-1.1获得要素POST请求, HTTP://data.wien.gv .AT /名单Daten /的GeoServer /世界粮食首脑会议

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 code我来到了现在。这主要是通过从ksoap2,机器人维基 例子inspirated 。我完全不确定是否命名空间方法名网址的是正确的!

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请求中使用的命名空间和连接它们我的项目。这使我能够装配该请求的对象。

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 包含在核心库(<$ C C $> javax.xml.bind.JAXBElement )的Andr​​oid的拒绝消费。次要问题陈述了意见和待办事项。

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.

编辑:五月09,2012

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

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

推荐答案

@JJD我看到你在<一个给我留下了味精href="http://stackoverflow.com/questions/8968620/android-soapfault-error/9042499#comment11653966_9042499">here
我有一段时间开会,但我看了看你的问题,并会很乐意帮助尽可能多的。我看到你有一个问题,阅读模式定义。此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

取方法获得性能,让我们读它在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


现在你已经获得性能的要求:


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>

获得性能有着复杂类型的类型:GetCapabilitiesType,你发现在XSD的一个环节此页,precisely owsGetCapabilities.xsd

- >打开后即: HTTP://schemas.opengis .NET / OWS / 1.0.0 / owsGetCapabilities.xsd

--> After opening it ie, : 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 =AcceptVersionsTYPE =OWS:AcceptVersionsType和的minOccurs =0,即可以为空 NAME =节TYPE =OWS:SectionsType和的minOccurs =0,即可以为空 NAME =AcceptFormatsTYPE =OWS:AcceptFormatsType和的minOccurs =0,即可以为空 NAME =UPDATESEQUENCE类型=OWS:UpdateSequenceType,其是可选项 - >使用=可选

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"

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

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.0 / owsCommon.xsd


so AcceptVersionsType has an element of type: VersionType can be found at xsd owsOperationsMetadata.xsd ( which is on the same link of this page) and on it you have xsd:owsCommon.xsd this is this where VersionType is found ie: 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>

和sectionsType是:

and sectionsType is:

<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

<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,你必须创建本地再presentations的类(对象)实现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).

现在,你可以阅读我的​​回答就知道该怎么做的: <一href="http://stackoverflow.com/questions/9023442/android-wsdl-web-service-ksoap2/9042267#9042267">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.

我不会在我的电脑的一天。希望这可以帮助,让我知道,如果重量的什么,我说的是ambigous。

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天全站免登陆