title: Sending a pdf file to client from server through web service
body: <p>As I am new to web service can you please tell the answer of my question.
  My question is</p>
  
  <blockquote>
    <p>I want to implement a web service which sends pdf files to client as the client calls this web service.</p>
  </blockquote>
  
  <p>Please someone help me with nice piece of code or explanation.</p>
  
  <p>Now can some one please solve my error.</p>
  
  <blockquote>
    <p>12-23 09:42:48.429: DEBUG/installd(32): DexInv: --- BEGIN '/data/app/vmdl33143.tmp' ---
    12-23 09:42:51.708: DEBUG/dalvikvm(328): DexOpt: 'Lorg/xmlpull/v1/XmlPullParser;' has an earlier definition; blocking out<br>
    12-23 09:42:51.730: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/io/KXmlParser;' has an earlier definition; blocking out<br>
    12-23 09:42:51.749: DEBUG/dalvikvm(328): DexOpt: 'Lorg/xmlpull/v1/XmlSerializer;' has an earlier definition; blocking out<br>
    12-23 09:42:51.759: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/io/KXmlSerializer;' has an earlier definition; blocking out<br>
    12-23 09:42:51.779: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/kdom/Node;' has an earlier definition; blocking out<br>
    12-23 09:42:51.779: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/kdom/Document;' has an earlier definition; blocking out<br>
    12-23 09:42:51.819: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/kdom/Element;' has an earlier definition; blocking out<br>
    12-23 09:42:51.830: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/wap/Wbxml;' has an earlier definition; blocking out<br>
    12-23 09:42:51.840: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/wap/WbxmlParser;' has an earlier definition; blocking out<br>
    12-23 09:42:51.900: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/wap/WbxmlSerializer;' has an earlier definition; blocking out<br>
    12-23 09:42:51.910: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/wap/syncml/SyncML;' has an earlier definition; blocking out<br>
    12-23 09:42:51.910: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/wap/wml/Wml;' has an earlier definition; blocking out<br>
    12-23 09:42:51.929: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/wap/wv/WV;' has an earlier definition; blocking out<br>
    12-23 09:42:51.929: DEBUG/dalvikvm(328): DexOpt: 'Lorg/xmlpull/v1/XmlPullParserException;' has an earlier definition; blocking out<br>
    12-23 09:42:51.949: DEBUG/dalvikvm(328): DexOpt: 'Lorg/xmlpull/v1/XmlPullParserFactory;' has an earlier definition; blocking out<br>
    12-23 09:42:53.299: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'<br>
    12-23 09:42:53.319: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'<br>
    12-23 09:42:53.329: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'<br>
    12-23 09:42:53.350: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'<br>
    12-23 09:42:53.350: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'<br>
    12-23 09:42:53.370: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'<br>
    12-23 09:42:53.499: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'<br>
    12-23 09:42:53.560: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'<br>
    12-23 09:42:53.570: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/kxml2/io/KXmlSerializer;'<br>
    12-23 09:42:53.659: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/xmlpull/v1/XmlPullParser;': multiple definitions<br>
    12-23 09:42:53.668: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/io/KXmlParser;': multiple definitions<br>
    12-23 09:42:53.668: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/xmlpull/v1/XmlSerializer;': multiple definitions<br>
    12-23 09:42:53.668: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/io/KXmlSerializer;': multiple definitions<br>
    12-23 09:42:53.680: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/kdom/Node;': multiple definitions<br>
    12-23 09:42:53.690: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/kdom/Document;': multiple definitions<br>
    12-23 09:42:53.709: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/kdom/Element;': multiple definitions<br>
    12-23 09:42:53.709: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/wap/Wbxml;': multiple definitions<br>
    12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/wap/WbxmlParser;': multiple definitions<br>
    12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/wap/WbxmlSerializer;': multiple definitions<br>
    12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/wap/syncml/SyncML;': multiple definitions<br>
    12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/wap/wml/Wml;': multiple definitions<br>
    12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/wap/wv/WV;': multiple definitions<br>
    12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/xmlpull/v1/XmlPullParserException;': multiple definitions<br>
    12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/xmlpull/v1/XmlPullParserFactory;': multiple definitions<br>
    12-23 09:42:53.829: DEBUG/dalvikvm(328): DexOpt: load 867ms, verify 1752ms, opt 108ms<br>
    12-23 09:42:54.029: DEBUG/installd(32): DexInv: --- END '/data/app/vmdl33143.tmp'(success) ---</p>
  </blockquote>
  
  <p>This the Error in DDMS of android emulator.</p>
  
  <p>Now my web service &amp; code looks likes this.</p>
  
  <p>Code:-</p>
  
  <pre><code>package com.uks.webservice;
  
  import java.io.FileNotFoundException;  
  import java.io.IOException;  
  import java.rmi.Remote;  
  
  public interface SendPDF extends Remote {
  
      public String[] sendPDF() throws FileNotFoundException, IOException,
              Exception;
  }
  </code></pre>
  
  <p>Main Class from web service is created:-</p>
  
  <pre><code>package com.uks.webservice;
  
  import java.io.File;   
  import java.io.FileNotFoundException;  
  import java.io.IOException;  
  import java.io.InputStream;  
  
  import javax.activation.DataHandler;  
  import javax.activation.FileDataSource;  
  
  import sun.misc.BASE64Encoder;  
  
  public class SendPDFImpl implements SendPDF {
  
  @Override
  public String[] sendPDF() {
  
      String[] pdfFile = new String[2];
      BASE64Encoder encoder = new BASE64Encoder();
      File file = new File(
              "C:/eclipse/workspace/AIPWebService/src/test.pdf");
      DataHandler dh = new DataHandler(new FileDataSource(file));
      String readLine = null;
      byte[] data = new byte[(int) file.length()];
      int offset = 0;
      int numRead = 0;
      InputStream stream = null;
      try {
          System.out.println("Web Service Called Successfully");
          stream = dh.getInputStream();
          // BufferedReader bufferedReader = new BufferedReader(
          // new InputStreamReader(stream));
          while (offset &lt; data.length
                  &amp;&amp; (numRead = stream.read(data, offset, data.length
                          - offset)) &gt;= 0) {
              offset += numRead;
          }
          readLine = encoder.encode(data);
          // while ((readLine = bufferedReader.readLine()) != null) {
          System.out.println("'Reading File............................");
          System.out.println("\n");
          System.out.println("Data Reading Successful");
  
          pdfFile[0] = file.getName();
          pdfFile[1] = readLine;
  
          stream.close();
  
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } catch (Exception e) {
          e.printStackTrace();
      }
      return pdfFile;
  }
  }
  </code></pre>
  
  <p>Web Service:- </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;wsdl:definitions targetNamespace="http://webservice.uks.com"
      xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://webservice.uks.com"
      xmlns:intf="http://webservice.uks.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
      xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
      &lt;!--
          WSDL created by Apache Axis version: 1.4 Built on Apr 22, 2006
          (06:55:48 PDT)
      --&gt;
      &lt;wsdl:types&gt;
          &lt;schema elementFormDefault="qualified" targetNamespace="http://webservice.uks.com"
              xmlns="http://www.w3.org/2001/XMLSchema"&gt;
              &lt;element name="sendPDF"&gt;
                  &lt;complexType /&gt;
              &lt;/element&gt;
              &lt;element name="sendPDFResponse"&gt;
                  &lt;complexType&gt;
                      &lt;sequence&gt;
                          &lt;element maxOccurs="unbounded" name="sendPDFReturn" type="xsd:String" /&gt;
                      &lt;/sequence&gt;
                  &lt;/complexType&gt;
              &lt;/element&gt;
          &lt;/schema&gt;
      &lt;/wsdl:types&gt;
  
      &lt;wsdl:message name="sendPDFRequest"&gt;
  
          &lt;wsdl:part element="impl:sendPDF" name="parameters" /&gt;
  
      &lt;/wsdl:message&gt;
  
      &lt;wsdl:message name="sendPDFResponse"&gt;
  
          &lt;wsdl:part element="impl:sendPDFResponse" name="parameters" /&gt;
  
      &lt;/wsdl:message&gt;
  
      &lt;wsdl:portType name="SendPDFImpl"&gt;
  
          &lt;wsdl:operation name="sendPDF"&gt;
  
              &lt;wsdl:input message="impl:sendPDFRequest" name="sendPDFRequest" /&gt;
  
              &lt;wsdl:output message="impl:sendPDFResponse" name="sendPDFResponse" /&gt;
  
          &lt;/wsdl:operation&gt;
  
      &lt;/wsdl:portType&gt;
  
      &lt;wsdl:binding name="SendPDFImplSoapBinding" type="impl:SendPDFImpl"&gt;
  
          &lt;wsdlsoap:binding style="document"
              transport="http://schemas.xmlsoap.org/soap/http" /&gt;
  
          &lt;wsdl:operation name="sendPDF"&gt;
  
              &lt;wsdlsoap:operation soapAction="" /&gt;
  
              &lt;wsdl:input name="sendPDFRequest"&gt;
  
                  &lt;wsdlsoap:body use="literal" /&gt;
  
              &lt;/wsdl:input&gt;
  
              &lt;wsdl:output name="sendPDFResponse"&gt;
  
                  &lt;wsdlsoap:body use="literal" /&gt;
  
              &lt;/wsdl:output&gt;
  
          &lt;/wsdl:operation&gt;
  
      &lt;/wsdl:binding&gt;
  
      &lt;wsdl:service name="SendPDFImplService"&gt;
  
          &lt;wsdl:port binding="impl:SendPDFImplSoapBinding" name="SendPDFImpl"&gt;
  
              &lt;wsdlsoap:address
                  location="http://localhost:8080/AIPWebService/services/SendPDFImpl" /&gt;
  
          &lt;/wsdl:port&gt;
  
      &lt;/wsdl:service&gt;
  
  &lt;/wsdl:definitions&gt;
  </code></pre>
  
  <p>And Now My Android Code.</p>
  
  <pre><code>package com.uks.android.webservice;
  
  import android.app.Activity;  
  import android.os.Bundle;  
  import android.widget.TextView;  
  
  import org.ksoap2.SoapEnvelope;  
  import org.ksoap2.serialization.SoapObject;  
  import org.ksoap2.serialization.SoapSerializationEnvelope;  
  import org.ksoap2.transport.AndroidHttpTransport;  
  
  public class PDFActivity extends Activity {  
      private final String METHOD_NAME = "sendPDF";  
      private final String NAMESPACE = "http://webservice.uks.com/";  
      private final String SOAP_ACTION = NAMESPACE + METHOD_NAME;  
      private final String URL = "http://192.168.1.123:8080/AIPWebService/services/SendPDFImpl";  
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          TextView textViewOne = (TextView) findViewById(R.id.textViewOne);
          TextView textViewTwo = (TextView) findViewById(R.id.textViewTwo);
          try {
              SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);
  
              SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                      SoapEnvelope.VER11);
              envelope.setOutputSoapObject(soapObject);
  
              textViewOne.setText("Web Service Started");
              AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);
  
              httpTransport.call(SOAP_ACTION, envelope);
  
              SoapObject result = (SoapObject) envelope.getResponse();
              textViewTwo.setText(result.toString());
              // Object result = envelope.getResponse();
              // FileOutputStream outputStream = openFileOutput(name, mode)
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Please help me.</p>
  
  <p>Thanks in advance.  </p>