Link to home
Start Free TrialLog in
Avatar of rmartes
rmartes

asked on

How to send XML to HTTP server

Hey Experts,

I'm trying to send simple xml data/file to my HTTP server via my android app. My http server is already receiving XMl data from other services, so I know it's not my XML receiver. I found this example (attached as code):


However, I can not get this to work. Is the above the correct way of sending a string as XML, or is there some encoding involved? Can someone provide me with a working example?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of simonet
simonet
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rmartes
rmartes

ASKER

Thank you...I'll give them a try.

I quickly skimmed through and noticed that both examples force me to create and save an XML file to SDCARD...correct? If so, is that the only way to post XML? Can I just send the XML as a string like my example?
Yes. Try this:
 
 
public void postData(String sendData) {
        // Create a new HttpClient and Post Header
        System.out.println("SENDDATA" + sendData);
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www......");

    try {
            StringEntity se = new StringEntity(sendData);
            httppost.setEntity(se);
            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            System.out.println("Message Sent!");
            inputStreamToString(response.getEntity().getContent());
            }         catch (ClientProtocolException e) 
        {
            // TODO Auto-generated catch block
        } 
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
        }
    }

Open in new window


If your XML is too large for the string variable, you have to replace the FileInputStream for a InputStream, so you can read the XML from memory.

Avatar of rmartes

ASKER

No luck on the code above. My XML file is small (about 10 nodes). Im feel like I'm doing something wrong on the xml creation part:

String xmlString = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
                             "<form>
                             "<item1>blah</item1>" +
                             "</form>";

That's the correct way of forming XML as a string correct?
There is a missing quote on the second line, but other than that it seems ok.

Is the string being sent?

Can you tell me what the log output is on the Eclipse DDMS  when the procedure is run?

Avatar of rmartes

ASKER

The changed the system.out to log.

Logcat does not show any errors regarding the xml post. I see "SENDDATA" & "Message Sent".
Can you put some debug messages on the server that receives the XML file ?
Avatar of rmartes

ASKER

Unfortunately I can't as it is in production and I avoid touching the code once its working. I dont have a testing environment for the receiver because it is on of the situations where testing has to be done with live data. Any other suggestions?
Avatar of rmartes

ASKER

However, I do have an error logger on the receiver already that catches any exception. I did not see any errors though.
Avatar of rmartes

ASKER

My XML receiver is coded in ASPX and accepts an XML file not a url parameter. Does this mean I would have to grab and post the file from the SDCARD? or can I post the xml as string (not a file)?
You can write the XML as a string directly to the outputstream of the connection.  This puts the XML effectively as the body of the http request.  On the server side, you retrieve it with inputstream.  That's largely the same for .Net as well.

However, since you say you can't debug the server side, then I'm not sure what anyone can do.

Try creating a test server listener so that you can debug and get help from the experts on EE.
Avatar of rmartes

ASKER

Ok. MrCoffee, I did what you suggested and created some test environments in PHP as well as ASPX

I am able to receive the xml file using php. However, in ASPX, when I try using XmlDocument to read in the xml nodes I get the following error:

Data at the root level is invalid. Line 1, position 1.

Here is the exact generated xml file:

<?xml version="1.0" encoding="UTF-8" ?>
<webform>
<name>TEST</name>
<birthDate>02/01/98</birthDate>
<address>TEST</address>
<state>Alaska</state>
<zipcode>12233</zipcode>
<phone>123-123-1233</phone>
<email>gvarona@bindermis.com</email>
<stillWorking>Yes</stillWorking>
<lastDateOfWork>02/01/98</lastDateOfWork>
<jobDescription>TEST</jobDescription>
<hasApplySSD>Yes</hasApplySSD>
<underCareOfDoctor>No</underCareOfDoctor><detailedDescription>TEST. Please Delete</detailedDescription>
<referralType>Mobile</referralType>
</webform>

I was able to parse the xml using IE so I know it is wellformed. What could be the cause of the error above?
Great -- I'm glad you made a test listener.

So many things can cause a problem.  .Net returns that error as a general error message, apparently, on XML read calls.  Post your aspx code which reads the input and gets the error message.

Several things could be going wrong.  There could be white space at the beginning of your xml (IE is much more forgiving of badly formed xml than all the parsers I know).  You could be sending UTF-8 but not reading it as UTF-8 -- depending on the call you use to read the input, the default might be UTF-16, so it would fail with that error message.  You could actually have a not allowed IP address for your post, and that causes that error message (!).


Avatar of rmartes

ASKER

Here is the simple xmldocument code:

 
Page.Response.ContentType = "text/xml"
			'Read XML posted via HTTP
			Dim reader As StreamReader = new StreamReader(Page.Request.InputStream)
			
			Dim xmlData As String = reader.ReadToEnd()
			reader.Close()
			
			Dim doc As New XmlDocument
			doc.LoadXml(xmlData)
			
			'name
			Dim nodeName As XmlNode = doc.SelectSingleNode("webform/name")
			Dim strName As String = Trim(UCase(nodeName.InnerText))
			
			'phone
			Dim nodePhone As XmlNode = doc.SelectSingleNode("webform/phone")
			Dim strPhone As String = Trim(nodePhone.InnerText)
			
			'email
			Dim nodeEmail As XmlNode = doc.SelectSingleNode("webform/email")
			Dim strEmail As String = Trim(UCase(nodeEmail.InnerText))
			
			'referral
			Dim nodeReferralType As XmlNode = doc.SelectSingleNode("webform/referralType")
			Dim strReferralType As String = UCase(nodeReferralType.InnerText)
				
			'birthDate
			Dim nodeBirthDate As XmlNode = doc.SelectSingleNode("webform/birthDate")
			Dim strBirthDate As String = nodeBirthDate.InnerText
			
			'address
			Dim nodeAddress As XmlNode = doc.SelectSingleNode("webform/address")
			Dim strAddress As String = Trim(nodeAddress.InnerText)
			
			'state
			Dim nodeState As XmlNode = doc.SelectSingleNode("webform/state")
			Dim strState As String = UCase(nodeState.InnerText)
			
			'zipcode
			Dim nodeZipcode As XmlNode = doc.SelectSingleNode("webform/zipcode")
			Dim strZipcode As String = Trim(nodeZipcode.InnerText)
			
			'stillWorking
			Dim nodeStillWorking As XmlNode = doc.SelectSingleNode("webform/stillWorking")
			Dim strStillWorking As String = nodeStillWorking.InnerText
			
			'lastDateOfWork
			Dim nodeLastDateOfWork As XmlNode = doc.SelectSingleNode("webform/lastDateOfWork")
			Dim strLastDateOfWork As String = nodeLastDateOfWork.InnerText
			
			'jobDescription
			Dim nodeJobDescription As XmlNode = doc.SelectSingleNode("webform/jobDescription")
			Dim strJobDescription As String = Trim(nodeJobDescription.InnerText)
			
			'hasApplySSD
			Dim nodeHasApplySSD As XmlNode = doc.SelectSingleNode("webform/hasApplySSD")
			Dim strHasApplySSD As String = nodeHasApplySSD.InnerText
			
			'underCareOfDoctor
			Dim nodeUnderCareOfDoctor As XmlNode = doc.SelectSingleNode("webform/underCareOfDoctor")
			Dim strUnderCareOfDoctor As String = nodeUnderCareOfDoctor.InnerText
			
			'detailedDescription
			Dim nodeDetailedDescription As XmlNode = doc.SelectSingleNode("webform/detailedDescription")
			Dim strDetailedDescription As String = Trim(nodeDetailedDescription.InnerText)

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In your case, the page input stream, not foo.xml
Avatar of rmartes

ASKER

Beautifully done guys.....

The xmlData had a whole bunch of symbols and white spaces. I got it work now!!!!

Thank you so much!!!!

I will split the point up between mrcoffee and simonet.
Avatar of rmartes

ASKER

Excellent!!
Thanks and good luck!
I am all for have the XML data previously saved to file, specially during the development phase. In that case, if a problem arises you simply do a "adb pull" from the PC, get the file, analyse it and see what is actually wrong.

After all, saving the file to the sdcard and reading for there will add at most 4 lines of code. Once it`s debugged, you stream the file directly from the String variable.

.Net is far from being my cup of tea, so I can`t help you much on the server side.