Domanda

My app queries the server for some data, the server runs the corresponding SQL statement and gets the result. Now, the result is serialized into XML using Simple XML API. My problem is that I want to send this xml data back to the app.

Until now I was first saving the XML file on a location on the server and making it available as a URL- http://localhost:8080/abc/123.xml Here is how I'm doing it exactly as of now, using Simple XML API:

Serializer serializer = new Persister();
GetCard_info_xml getCard_Object = new GetCard_info_xml(received_numbers);               
File result = new File("/home/user/workspace/xml_files/GetCard_xml_output/GetCard_xml_output_"+ receivedMobileString +".xml");
try {
     serializer.write(getCard_Object, result);
    } catch (Exception e) {
     e.printStackTrace();
    }

After this, I ask my app to just parse the url using the same Simple XML API. Like this:

URL GetCardXmlUrl = new URL("http://10.0.2.2:8080/getcardxml/GetCard_xml_output_"+ mobile +".xml");
URLConnection GetCardXmlConnection = GetCardXmlUrl.openConnection();
InputStream PhonebookInstream = new BufferedInputStream(GetCardXmlConnection.getInputStream());
Serializer PhoneBookSerializer = new Persister();

I realize this is not appropriate as the xml file is public and hence not safe at all as all the data becomes public too. Plus, to me it seems just hacky and not an appropriate solution.

How can I send this whole serialized file from server to the app like I would a simple string using something like OutputStreamWriter?

È stato utile?

Soluzione

Here's a simple pseude code:

Example example = ... // create your data 


// Get your writer (or outputstream to write data)
// Those my come from doPost(...) or doGet(...) method
HttpServletResponse response = ...
PrintWriter outWriter = respone.getWriter();

// Serialize to the writer / stream
Serializer ser = new Persister();
ser.write(example, outWriter);

If you implementation class extends HttpServlet you can get the outputstream / writer from the parameters of doPost() / doGet() method:

  • void doGet(HttpServletRequest request, HttpServletResponse response)
  • void doPost(HttpServletRequest request, HttpServletResponse response)

Now you can either use a Writer (response.getWriter()) or an OutputStream (response.getOutputStream).

The Serializer can write to both.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top