Pages

Thursday 1 August 2013

Uploading file to webservice using KSOAP


1st step: Get the file from the SDcard and assign that file in INPUTSTREAM.
2nd step: Write the file into BYTEARRAYOUTPUTSTREAM
3rd step: Convert that Stream into BYTEARRAY
4th step: Convert Bytearray into BASE64STRING


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

                InputStream is = null;

try {
is = new BufferedInputStream(new FileInputStream(
Environment.getExternalStorageDirectory()
.getAbsolutePath()
+ "/download/"
+ yourfilename));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

//http://ricky-tee.blogspot.in/2012/06/converting-from-file-to-byte-array.html?showComment=1362738218958#c5250835248744198937

ByteArrayOutputStream bos = new ByteArrayOutputStream();

                               try {
while (is.available() > 0) {
bos.write(is.read());
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

byte[] byteArray = bos.toByteArray();
                             
                                String base64= Base64.encodeToString(byteArray,
Base64.DEFAULT);

      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
  request.addProperty("str", base64);
  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();
                                     } catch (Exception e) {
e.printStackTrace();

}          

For Reference.

No comments:

Post a Comment