1

KSOAP2を使用すると、SOAP エンベロープ内で次の XML を生成できます。

<v:Envelope 
    xmlns:i="http://www.w3.org/1999/XMLSchema-instance"
    xmlns:d="http://www.w3.org/1999/XMLSchema"
    xmlns:c="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
    <v:Header />
    <v:Body>
        <ApplyItem xmlns="http://www.myurl.com/">
            <Item type="User" action="get" select="login_name"/>
        </ApplyItem>
     </v:Body>
 </v:Envelope>

そして、envelope.dotNet = false; を設定することで、この XML を取得できます。

<v:Envelope 
    xmlns:i="http://www.w3.org/1999/XMLSchema-instance"
    xmlns:d="http://www.w3.org/1999/XMLSchema"
    xmlns:c="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
    <v:Header />
    <v:Body>
        <n0:ApplyItem xmlns:n0="http://www.myurl.com/">
            <n0:Item type="User" action="get" select="login_name"/>
        </n0:ApplyItem>
    </v:Body>
</v:Envelope>

このように xmlns 属性にのみ「n0」プレフィックスが付いた同じ XML を取得するには、どうすればよいですか?

<ApplyItem xmlns:n0="http://www.myurl.com/">
     <Item type="User" action="get" select="login_name" />
</ApplyItem>

私が持っているコードは次のとおりです

public void TestWebService() {
/*xmlVersionTag = "";
* NAMESPACE = "http://www.myurl.com/";
* SOAPaction = "ApplyItem";
* MYSERVER = "http://myServer/webservice.aspx";
* DATABASE = "dbName";
* AUTHUSER = "admin";
* AUTHPASSWORD = "pwd"; */
SoapObject request = new SoapObject(NAMESPACE, SOAPaction);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                                            SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setAddAdornments(false);
envelope.implicitTypes = true;
envelope.addMapping(null, "Item", new String().getClass());

SoapObject aml = new SoapObject(NAMESPACE, "Item");
aml.addAttribute("type", "User");
aml.addAttribute("action", "get");
aml.addAttribute("select", "login_name");
request.addSoapObject(aml);
envelope.setOutputSoapObject(request);

HttpClient client = new DefaultHttpClient();

try {
    callWS(client, MYSERVER, SOAPaction, envelope);
    SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
    Log.i("Webservice Output", response.toString());

    } catch (Exception e) {
        e.printStackTrace();
    } 
return;
}

CallWS は、HttpClient を使用して Http ヘッダーに接続し、soap エンベロープから要求データをポストして、応答を解析してエンベロープ入力に戻します。

これがコードのその部分です。

public void callAras(HttpClient httpClient, 
       String url, String soapAction, SoapEnvelope envelope) 
       throws IOException, XmlPullParserException {
if (soapAction == null) {
    soapAction = "\"\"";
 }
 byte[] requestData = createRequestData(envelope);
 String requestDump = new String(requestData);

 HttpPost method = new HttpPost(url);

 method.addHeader("SOAPAction", soapAction);
 method.addHeader("AUTHUSER", AUTHUSER);
 method.addHeader("AUTHPASSWORD", AUTHPASSWORD);
 method.addHeader("DATABASE", DATABASE);

 HttpEntity entity = new ByteArrayEntity(requestData);
 method.setEntity(entity);
 HttpResponse response = httpClient.execute(method);
 InputStream inputStream = response.getEntity().getContent();
 parseResponse(envelope, inputStream);

 inputStream.close();
 }

私が受け取った障害コードは次のとおりです。

SOAP-ENV:Server.TagItemIsNotFoundInRequestException
There is no tag <Item> in request.
4

1 に答える 1

1

最初に、Web サービスで機能する要求 XML をいくつか以下に示します。「n0」プレフィックスが Item 要素から削除されたことに注意してください。

<v:Envelope 
    xmlns:i="http://www.w3.org/1999/XMLSchema-instance"
    xmlns:d="http://www.w3.org/1999/XMLSchema"
    xmlns:c="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
    <v:Header />
    <v:Body>
        <n0:ApplyItem xmlns:n0="http://www.myurl.com/">
            <Item type="User" action="get" select="login_name"/>
        </n0:ApplyItem>
    </v:Body>
</v:Envelope

私が犯した間違いは、SoapPrimitive クラスを使用して定義する代わりに、SoapObject を使用して Item 要素を定義したことです。コードの修正は次のとおりです。

envelope.dotNet = false;
envelope.setAddAdornments(false);
envelope.implicitTypes = true;

SoapPrimitive amlItem = new SoapPrimitive(NAMESPACE, "Item", "");
amlItem.addAttribute("type", "User");
amlItem.addAttribute("action", "get");
amlItem.addAttribute("select", "login_name");
request.addProperty("Item", amlItem);
envelope.setOutputSoapObject(request);

リクエストが機能したら、レスポンスのキャストを変更する必要がありました

SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

KSOAP によって返される「anytype」を処理するには、以下を参照してください。

Object response = (Object) envelope.getResponse();
于 2012-09-29T03:46:13.763 に答える