Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a pdf file to client from server through web service

As I am new to web service can you please tell the answer of my question. My question is

I want to implement a web service which sends pdf files to client as the client calls this web service.

Please someone help me with nice piece of code or explanation.

Now can some one please solve my error.

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
12-23 09:42:51.730: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/io/KXmlParser;' has an earlier definition; blocking out
12-23 09:42:51.749: DEBUG/dalvikvm(328): DexOpt: 'Lorg/xmlpull/v1/XmlSerializer;' has an earlier definition; blocking out
12-23 09:42:51.759: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/io/KXmlSerializer;' has an earlier definition; blocking out
12-23 09:42:51.779: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/kdom/Node;' has an earlier definition; blocking out
12-23 09:42:51.779: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/kdom/Document;' has an earlier definition; blocking out
12-23 09:42:51.819: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/kdom/Element;' has an earlier definition; blocking out
12-23 09:42:51.830: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/wap/Wbxml;' has an earlier definition; blocking out
12-23 09:42:51.840: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/wap/WbxmlParser;' has an earlier definition; blocking out
12-23 09:42:51.900: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/wap/WbxmlSerializer;' has an earlier definition; blocking out
12-23 09:42:51.910: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/wap/syncml/SyncML;' has an earlier definition; blocking out
12-23 09:42:51.910: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/wap/wml/Wml;' has an earlier definition; blocking out
12-23 09:42:51.929: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/wap/wv/WV;' has an earlier definition; blocking out
12-23 09:42:51.929: DEBUG/dalvikvm(328): DexOpt: 'Lorg/xmlpull/v1/XmlPullParserException;' has an earlier definition; blocking out
12-23 09:42:51.949: DEBUG/dalvikvm(328): DexOpt: 'Lorg/xmlpull/v1/XmlPullParserFactory;' has an earlier definition; blocking out
12-23 09:42:53.299: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
12-23 09:42:53.319: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
12-23 09:42:53.329: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
12-23 09:42:53.350: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
12-23 09:42:53.350: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
12-23 09:42:53.370: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
12-23 09:42:53.499: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
12-23 09:42:53.560: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
12-23 09:42:53.570: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/kxml2/io/KXmlSerializer;'
12-23 09:42:53.659: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/xmlpull/v1/XmlPullParser;': multiple definitions
12-23 09:42:53.668: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/io/KXmlParser;': multiple definitions
12-23 09:42:53.668: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/xmlpull/v1/XmlSerializer;': multiple definitions
12-23 09:42:53.668: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/io/KXmlSerializer;': multiple definitions
12-23 09:42:53.680: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/kdom/Node;': multiple definitions
12-23 09:42:53.690: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/kdom/Document;': multiple definitions
12-23 09:42:53.709: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/kdom/Element;': multiple definitions
12-23 09:42:53.709: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/wap/Wbxml;': multiple definitions
12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/wap/WbxmlParser;': multiple definitions
12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/wap/WbxmlSerializer;': multiple definitions
12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/wap/syncml/SyncML;': multiple definitions
12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/wap/wml/Wml;': multiple definitions
12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/wap/wv/WV;': multiple definitions
12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/xmlpull/v1/XmlPullParserException;': multiple definitions
12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/xmlpull/v1/XmlPullParserFactory;': multiple definitions
12-23 09:42:53.829: DEBUG/dalvikvm(328): DexOpt: load 867ms, verify 1752ms, opt 108ms
12-23 09:42:54.029: DEBUG/installd(32): DexInv: --- END '/data/app/vmdl33143.tmp'(success) ---

This the Error in DDMS of android emulator.

Now my web service & code looks likes this.

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;
}

Main Class from web service is created:-

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 < data.length
                && (numRead = stream.read(data, offset, data.length
                        - offset)) >= 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;
}
}

Web Service:-

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <!--
        WSDL created by Apache Axis version: 1.4 Built on Apr 22, 2006
        (06:55:48 PDT)
    -->
    <wsdl:types>
        <schema elementFormDefault="qualified" targetNamespace="http://webservice.uks.com"
            xmlns="http://www.w3.org/2001/XMLSchema">
            <element name="sendPDF">
                <complexType />
            </element>
            <element name="sendPDFResponse">
                <complexType>
                    <sequence>
                        <element maxOccurs="unbounded" name="sendPDFReturn" type="xsd:String" />
                    </sequence>
                </complexType>
            </element>
        </schema>
    </wsdl:types>

    <wsdl:message name="sendPDFRequest">

        <wsdl:part element="impl:sendPDF" name="parameters" />

    </wsdl:message>

    <wsdl:message name="sendPDFResponse">

        <wsdl:part element="impl:sendPDFResponse" name="parameters" />

    </wsdl:message>

    <wsdl:portType name="SendPDFImpl">

        <wsdl:operation name="sendPDF">

            <wsdl:input message="impl:sendPDFRequest" name="sendPDFRequest" />

            <wsdl:output message="impl:sendPDFResponse" name="sendPDFResponse" />

        </wsdl:operation>

    </wsdl:portType>

    <wsdl:binding name="SendPDFImplSoapBinding" type="impl:SendPDFImpl">

        <wsdlsoap:binding style="document"
            transport="http://schemas.xmlsoap.org/soap/http" />

        <wsdl:operation name="sendPDF">

            <wsdlsoap:operation soapAction="" />

            <wsdl:input name="sendPDFRequest">

                <wsdlsoap:body use="literal" />

            </wsdl:input>

            <wsdl:output name="sendPDFResponse">

                <wsdlsoap:body use="literal" />

            </wsdl:output>

        </wsdl:operation>

    </wsdl:binding>

    <wsdl:service name="SendPDFImplService">

        <wsdl:port binding="impl:SendPDFImplSoapBinding" name="SendPDFImpl">

            <wsdlsoap:address
                location="http://localhost:8080/AIPWebService/services/SendPDFImpl" />

        </wsdl:port>

    </wsdl:service>

</wsdl:definitions>

And Now My Android 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();
        }
    }
}

Please help me.

Thanks in advance.

like image 381
Shashank_Itmaster Avatar asked Dec 22 '10 07:12

Shashank_Itmaster


People also ask

Where can I host a PDF online so I can send people a link that they can view?

Google Drive Google Drive can help you publish your PDF documents publicly, which can then be shared to a wider audience. You can also embed the file on your website; access can be revoked at any time.


1 Answers

You need to convert file to byte array and pass it on to service on server do the reverse to get file back.

  • Here is very good article on doing this using JAXWS
like image 117
jmj Avatar answered Oct 04 '22 06:10

jmj