Accessing web service by Android application I usually use the Ksoap2 library and the code:
response.getProperty("field")
where "field" is the element name of XML, and response is the soapObject in which I have "captured" the WS answer.
The typical XML associate to this answer is:
<registry>
<cf>issjdeodk1292983ls</cf>
<name>John</name>
<surname>Doe</surname>
<sex>M</sex>
<message>a simple optional message</message>
</registry>
In this case when I try to access the field "message" there are no problems:
response.getProperty("message")
returns exactly the string "a simple optional message"
But my web service, if the message is not found (server side) return the following XML file
<registry>
<cf>issjdeodk1292983ls</cf>
<name>John</name>
<surname>Doe</surname>
<sex>M</sex>
</registry>
that is, the same previous XML without tag.
In this case if I use the code:
response.getProperty("message")
an error occurs.
Can I use a mechanism for getting the property only if exists?
I am not sure which library you use but when using xmlpullparser you can lookup if there is a tag like that by
if (parser.getName() == "message") { ....
Related
I have a link Where i want to send data in REST XML format, below is the sample xml that i must send. How make a xml structure like below and send it to the given link ? (Through Android application using JAVA)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE applicants PUBLIC "//National Informatics Center/" "../../files_uc09/llform.dtd">
<!-- Warning: Please Don't change the DTD declaration -->
<applicants>
<applicant refno="1">
<licence-type>l</licence-type>
<statecode>MH</statecode>
<rtocode>MH02</rtocode>
<applicant-name>
<first-name>Bhairas</first-name>
<middle-name>Rao</middle-name>
<last-name>ray</last-name>
</applicant-name>
<gender type="male"/>
<dob>1-07-1961</dob>
<birth-place>Warangal</birth-place>
..
.(and so on its big )
.
</applicants>
So How to fill my data in this type of xml format and send it to some link.
Note: I cannot control any thing on server side. I can just send in prescribed format and receive response .
-Using these two class:-
DocumentBuilderFactory andTransformerFactory you can post your data to server in xml format
You can try amazing Retrofit library http://square.github.io/retrofit/. Check out "Content format Agnostic" section
This question was first titled: How to remove -xmlnl tags when I receive JSON from a XML-HTTP-API server? but now I understand the concept of the problem.
This question is about a exception in Android regarding JSON translation. I do a HTTP GET request to a (not-mine) webserver made with c# and some api that, when I call it from a browser, the answer comes in XML and when I call it from my Android application the result is in JSON.
The thing is I am getting an JSON error like: Value {"-xmlnl:d4p1":"http://schemas..."} of Alternativas of type org.json.JSONObject cannot be converted to JSONArray
The code in XML has this king of content:
<Alternativas xmlns:d4p1="http://schemas.datacontract.org/2004/07/PainelMonitor.Model"/>
<Descricao>A primeira fraze que vocĂȘ ouviu hoje foi?</Descricao>
I am only interested in the <Descricao> content.
This {"-xmlnl:d4p1":"http://schemas..."} should not come to JSON and it maked my app crash.
So how could I take it off from my JSON? There are 'tens' of these invalid markups in the code, so I can't do it manually because it is a Array of JSON.
In your xsl you have to use **
exclude-result-prefixes=""
** attribute.
The soap that I want is like this:
<int:userId>......</int:userId>
This is tested in SoapUI and with this I get the proper response back.
What Ksoap makes is this:
<userId i:type="n0:undefined" xmlns:n0="http://namespace.com">.....</userId>
If I where to paste this code into SoapUI I get an error
'<Message>Error in line 5 position 88. Element 'http://namespace.com:
userId'
contains data of the 'http://namespace.com:undefined' data contract.
The deserializer has no knowledge of any type that maps to this contract.
Add the type corresponding to 'undefined' to the list of known types -
for example, by using the KnownTypeAttribute attribute or by
adding it to the list of known types passed to DataContractSerializer.
</Message>
Maybe I am looking in the wrong direction for the solution but I am now asking how can I make ksoap omit the i:type part. If anyone can provide a different sollution, you will have my thank.
KSoap does have a tendancy to insert its own namespaces into each tag, which can give the output you're seeing.
I had a similar such problem, which was easily resolved using the following :
envelope.implicitTypes = true;
Please also have a look at this other question on SO.
The title maybe a little confusing so I shall explain a lot more details here. First off, I have an URL link that uses HTTPS secured site. I already managed a successful attempt to use a WebView to connect to the site, using loadDataWithBaseURL method.
The URL link does not end with .xml, so I am stuck with this problem. What are the procedures to do in order I can use the xml data on an URL link that does not ends with .xml?
EDIT:
<MGMT>
<NET>
<HEAD>
<ClientID>99999999</ClientID>
<ServerID>WEB_01</ServerID>
<Rsp>00</Rsp>
<Auth></Auth>
</HEAD>
<STAT>
<IP>192.168.5.158</IP>
<Status>OK, Success!</Status>
</NET>
</MGMT>
I do have XML parsing knowledge. But that is if the url link returns a XML data or I have an XML file. This is NOT THE ACTUAL LINK but it is similar, I changed a few values in it.
https://192.168.0.254/?ClientID=999999&Cert=0f7a248e3b017effec2b36cf53912b0f&IP=192.168.5.128
Ok. As i come to know about your question, you have to gain some knowledge about xml parsing which convert the fetched XML data in to specific variables.
Here in below examples, the project get the Http responce from the server which is in the formate of the xml file. (Which URL not having .XML extention). And that data are stored in to specific variable.
You have to fetch various data based on the tab as per your xml structure. as like "MGMT", "NET" . . .etc etc. . .
See this: Example 1
Also see this: Exapmple 2
Hope you got the Logic to do it.
I have to get xml data from a .net web service. The webservice returns an XMLDocument object. I have been unable to successfully retrive/interpret the data as XML with start tags etc. It looks like this
MethodResponse{MethodResult=anyType{BUILDINGPERMITS=anyType{PERMITS=anyType{APNO= 1829; YCOORD=36.09593993; XCOORD=-80.24505836; JOBADDRESS=200 W SECOND ST WINSTON-SALEM 27101-; STATUS=Closed; }; ...
I can succssfully retrieve XML data if it is sent as a string rather than XMLDocument.
Is there a way to get XML data from a webservice that returns a .net object like XMLDocument or is that just not practical?
I think you will be better off retrieving the xml data as a String. See working with xml in android for a variety of ways of dealing with xml data.