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
Related
I have some sort of simple mobile shopping section inside my app. I get XML or JSON from server and want to put name/description/price/currency/etc to corresponding views. I really don't want to parse all XML myself and prefer to use some syntax like ["products.product.price" -> R.id.priceTextView] to map xml/json data to Android views.
I googled a little bit but failed to find a good way to do this. Do anybody know some lib or maybe Android class doing that?
i have create a protobuf sample code in android as follows
Person john =
Person.newBuilder()
.setId(1234)
.setName("John Doe")
.setEmail("jdoe#example.com")
.addPhone(
Person.PhoneNumber.newBuilder()
.setNumber("555-4321")
.setType(Person.PhoneType.HOME))
.build();
now i want to send this john object as a part of xml building block over network
so far i have seen following methods so that i can send bytes over network
john.toByteArray() and john.toByteString()
but i think when i will embed into xml tag as follows it will be the string representation only and from that i can not get the data back
"<data>" + john.toByteArray() + "</data>"
so how can i pass the protobuf message with XML ?
note: i don't want to use the base64 encoding as it will eventually increasing the size of a block
The fundamental problem here is that protobuf encoding is binary while XML is text. You can't embed binary data directly into text; you need to encode it as text somehow.
Unfortunately, there is simply no way to do that without increasing the data size. If size is your concern, base64 is likely your best option -- this is exactly what it was designed to do.
Another possibility would be to encode the message in protobuf text format using .toString() and then parse it using com.google.protobuf.TextFormat. This will produce a human-readable encoding, but it will be much larger than the binary encoding.
Yet another option would be to write a custom translator which uses the protobuf reflection iterfaces (e.g. com.google.protobuf.Message#getField()) to read individual fields and convert them to nested XML elements. However, this is complicated and will probably end up taking even more space than protobuf text format.
These are the options that I'm aware of:
using this 3rd party library.
With this method, you can generate xml that you can embed in your outer xml
using the TextFormat API, though parsing seem to be possible only in c.
from protobuf to string format:
TextFormat.shortDebugString((myProtobufMessage);
from string format to protobufm in c++ code
TextFormat:parseFromString(dataString, &myProtobufMessage);
(I didn't try this myself, but I saw this reference).
With this method you generate a String that you can embed in your XML, and on the receiving end, take that String and convert it to protobuf Message objects.
using protobuf > binary > base64 - instead of xml altogether. You can probably send all the data in the wrapping XML inside the protobuf message. This is what I do.
With this method you can forget about XML and use protobuf for everything. This is the original purpose of protobuf, and this is what it is best for.
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") { ....
I am trying to convert data from database to XML in android. After that i am planning to post xml data to server using HttpPost?. But i am not sure how to convert string to xml in android.
I am looking for any sample code and methods.
Thanks in advance
Use the DOM from java library.
Refer:
http://developer.android.com/reference/javax/xml/parsers/DocumentBuilder.html#parse(java.lang.String)
http://exampledepot.com/egs/javax.xml.parsers/BasicDom.html
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.