Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving bytes from a .NET WebService from Android
    text
    copied!<p>I have an Android application that calls an .NET WebService that sends back a base64 binary encoded bytes (it's a PDF file).</p> <p>My goal is to save these bytes as a PDF file on the device's sdcard.</p> <p>Here is (a small part) the response I get from the WebService :</p> <pre><code>&lt;base64Binary xmlns="https://www.abcd.com"&gt;JVBERi0xLjMKJdP0zOEKM....&lt;/base64Binary&gt; </code></pre> <p>On my application side, I call this WebService using a POST (for authentification purpose) request. I then get the result and parse it with XmlPullParser. The node value (the bytes) is stored in a StringBuilder (<strong>is it a good choice?</strong>) and I save the file like this :</p> <pre><code>try { FileOutputStream fos = new FileOutputStream(path + "/" + filename); fos.write(nodeValue.toString().getBytes()); fos.close(); } catch(FileNotFoundException ex) { System.out.println("FileNotFoundException : " + ex); } catch(IOException ioe) { System.out.println("IOException : " + ioe); response = false; } </code></pre> <p>When I extract the created PDF file with DDMS, and try to open it with Acrobat Reader, I'm getting an error that the file isn't in the right format.</p> <p>Any idea?</p> <p>======= ANSWER =======</p> <p>To solve my problem, I used the <a href="http://code.google.com/p/ksoap2-android/" rel="nofollow">kSoap2</a> library to get the result from the WebService as a SOAP response. Here is how it did the trick.</p> <pre><code>private static final String SOAP_ACTION = "https://www.foobar.com/getFile"; private static final String METHOD_NAME = "getFile"; private static final String NAMESPACE = "https://www.foobar.com"; private static final String URL = "http://www.foobar.com/ws.asmx"; try { SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("username", "foo"); request.addProperty("password", "bar"); request.addProperty("file", "foo.pdf"); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.setOutputSoapObject(request); envelope.dotNet = true; HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); androidHttpTransport.call(SOAP_ACTION, envelope); // Get the result SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn; byte[] result = Base64.decode(resultsRequestSOAP.getProperty("getFileResult").toString()); // Save the file try { FileOutputStream fos = new FileOutputStream("/mnt/sdcard/documents/foo.pdf"); fos.write(result); fos.close(); } catch(FileNotFoundException ex) { System.out.println("FileNotFoundException : " + ex); } catch(IOException ioe) { System.out.println("IOException : " + ioe); } } catch (Exception e) { e.printStackTrace(); } </code></pre> <p>The line :</p> <pre><code>byte[] result = Base64.decode(resultsRequestSOAP.getProperty("result").toString()); </code></pre> <p>decodes the result from the String. And the, the byte[] array is saved in a file with :</p> <pre><code>fos.write(result); </code></pre> <p>Voilà! Here are the complete code for folks who have the same problem. This is the WebService signature I call :</p> <pre><code>POST /wsInspection.asmx HTTP/1.1 Host: www.foobar.com Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "https://www.foobar.com/getFile" &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt; &lt;soap:Body&gt; &lt;getFile xmlns="https://www.foobar.com"&gt; &lt;username&gt;string&lt;/username&gt; &lt;password&gt;string&lt;/password&gt; &lt;file&gt;string&lt;/file&gt; &lt;/getFile&gt; &lt;/soap:Body&gt; </code></pre> <p></p> <p>Response :</p> <pre><code>HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt; &lt;soap:Body&gt; &lt;getFileResponse xmlns="https://www.foobar.com"&gt; &lt;getFileResult&gt;base64Binary&lt;/getFileResult&gt; &lt;/getFileResponse&gt; &lt;/soap:Body&gt; &lt;/soap:Envelope&gt; </code></pre> <p>Hope it helps! Enjoy.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload