Pages

Thursday 1 August 2013

Downloading file from webservice using KSOAP

Here is the code to download the file from webservice.

Coding:

                final String SOAP_ACTION = "";//Your soap action
final String METHOD_NAME = "";//Your method name
final String NAMESPACE = "";//Your name space
final String URL = "";//Your url


                                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                                //If you have some parameter to pass
//request.addProperty("", "");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);

HttpTransportSE ht = new HttpTransportSE(URL);

try {
ht.call(SOAP_ACTION, envelope);
final SoapPrimitive response = (SoapPrimitive) envelope
.getResponse();

String str = response.toString();

                                        //converting string to byte[]

byte[] decodedString = Base64.decode(str, Base64.DEFAULT);

                                  
File direct = new File(
Environment.getExternalStorageDirectory()
+ "/Download/");

if (!direct.exists()) {
direct.mkdir();
}

File file = new File(
Environment.getExternalStorageDirectory()
+ "/Download/", yourfilename);
    
if (file.exists()) {
file.delete();
}
                                              
try {
                                        //http://stackoverflow.com/a/7982964/1835764

FileOutputStream fos = new FileOutputStream(
file.getPath());

fos.write(decodedString);
fos.close();
                                             } catch (java.io.IOException e) 
                                             {
Log.e("fileDemo", "Exception in fileCallback", e);
}
}

catch (Exception e) {
e.printStackTrace();
}
}

}

No comments:

Post a Comment