Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Sending XML to PHP Service via POST

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Sending XML to PHP Service via POST

    I have a server with a handful of php services. I communicate with these services by creating connections from java code. The services currently respond with XML, but that can be changed if an alternative way can improve performance.
    One of the things I need to do is send varying sized payload (some large) to one of the services. My current plan is to create an xml file in java and perform a POST request with the XML as the payload. I am using an XML because the data is very structured.

    From a performance point of view, would it be better to send the XML as a file or just as text?
    Also, is there a better way to send this data? These services will eventually be used by an android app, where performance and minimizing the size of the data packages will be extremely important.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    276
    My Mood
    Bored
    Thanks
    39
    Thanked 76 Times in 64 Posts
    Blog Entries
    4

    Default Re: Sending XML to PHP Service via POST

    I used to rely on XML a lot for cross platform communications (even got a perfect 100% in markup languages at uni so I do love XML, XSLT & DTD's).

    These days I only ever use JSON, it's a no brainer. JSON is human readable, valid Javascript code, maps nicely to Hashmap <Key, Value> pairs or nested data and has well tested libraries for all major languages. It's a little lighter on size but the real advantage is you can throw away those complex DOM parsers and any invalid data structures are self contained and don't effect the entire data model. Reflection is easier, unit tests are easier and cross platform ambiguities are non existant.

    Quote Originally Posted by aussiemcgr
    These services will eventually be used by an android app, where performance and minimizing the size of the data packages will be extremely important.
    Just keep your web service calls as async tasks. In my experiences with very large vector datasets you can get away with several megabytes of data before parsing becomes another async task.]

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Re: Sending XML to PHP Service via POST

    I'll have to look into that. I've used JSON at work before, but never on my personal time.
    One of my services returned a 128kb (I think) XML result to my android app, and it took 10 minutes or so until the app just gave up (timeout or something I think). It was reading the XML, but EXTREMELY slowly. I was just using the standard DOM parser, so that probably didn't help expedite things. It was also an emulator instead of a real phone. Still, insanely slow. I was running it on another task, so nothing was locked up.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Forum VIP
    Join Date
    Jun 2011
    Posts
    276
    My Mood
    Bored
    Thanks
    39
    Thanked 76 Times in 64 Posts
    Blog Entries
    4

    Default Re: Sending XML to PHP Service via POST

    10 minutes just for deserialization!? I'd hazard a guess and say there is a problem in your implementation. There is no reason it should take that long.

    On an unrelated note if you are stuck testing without a physical device I strongly recommend Genymotion. It's a virtual machine and much faster than the simulator.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Re: Sending XML to PHP Service via POST

    Well it only takes that long when attempting to do it from the Android emulator. I have an android platform and a desktop platform. The two call the exact same code, which performs the request to the service, deserializes, and loads the data into the same data structures. Nothing is different between the two. Except that the Android platform takes forever, while the desktop platform is milliseconds. It's on my to-do list to do some research into what Android is actually doing differently when it parses xml.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #6
    Forum VIP
    Join Date
    Jun 2011
    Posts
    276
    My Mood
    Bored
    Thanks
    39
    Thanked 76 Times in 64 Posts
    Blog Entries
    4

    Default Re: Sending XML to PHP Service via POST

    Your shared code.. does it use any of the new Java 8 features like lambdas? Android won't support them.

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Re: Sending XML to PHP Service via POST

    No. It is still compiled in Java SE 7 and the runtime is still Java SE 7. It shouldn't have any Java 8 stuff.
    I use a DocumentBuilder created by the DocumentBuilderFactor (which I have set to be an instance of com.sun.org.apache.xerces.internal.jaxp.DocumentBu ilderFactoryImpl).
    It parses the URL with the InputStream provided by the openStream() method (URL (Java Platform SE 7 )). That parses the info into org.w3c.dom objects.

    /**
    	 * Returns a Document from the give URL
    	 * @param xml The url which contains the xml
    	 * @return A document of the URL
    	 * @throws ParserConfigurationException if the {@link DocumentBuilderFactory} implementation is not available or cannot be instantiated
    	 * @throws IOException if an I/O exception occurs
    	 * @throws SAXException if any parse errors occur
    	 */
    	public static Document loadXMLFromURL(URL xml) throws ParserConfigurationException, IOException, SAXException {
    		System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"); 
    		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
     
    	    factory.setNamespaceAware(true);
    	    DocumentBuilder builder = factory.newDocumentBuilder();
    	    return builder.parse(xml.openStream());
    	}
    The bottle neck appears to be continuous as it reads elements, not just an initial load thing.
    I need to do more testing before I can figure out a proper cause.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. PHP Developer - OOP / MVC / XML / JSON - 40,000 (NEG)
    By JacquelineRobbins in forum Paid Java Projects
    Replies: 0
    Last Post: January 12th, 2012, 05:58 AM
  2. Method :sending image java server to php client
    By ashi123 in forum Java Theory & Questions
    Replies: 0
    Last Post: July 22nd, 2011, 10:15 AM
  3. Replies: 1
    Last Post: April 26th, 2011, 02:54 AM
  4. [SOLVED] Sending xml file over a socket
    By Kakashi in forum Java Networking
    Replies: 2
    Last Post: March 9th, 2011, 08:41 AM
  5. Sending XML over HTTP to Another Application
    By darasgar in forum Java Servlet
    Replies: 2
    Last Post: July 14th, 2010, 01:56 PM