Pages

Showing posts with label ksaop. Show all posts
Showing posts with label ksaop. Show all posts

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.

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

}

Friday, 26 July 2013

Consume .net webservice in android



This simple example demonstrates how we can access a .net web service  from Android application.

 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/web_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="112dp"
        android:layout_marginTop="146dp"
        android:text="Web Service" />

    <TextView
        android:id="@+id/fetch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="TextView" />

    <ImageView
        android:id="@+id/fetch_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/fetch"
        android:layout_below="@+id/fetch"
        android:layout_marginTop="52dp"
        android:src="@drawable/ic_action_search" />

</RelativeLayout>

Activity:

public class MainActivity extends Activity {

Button web_service;
TextView fetch_service;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

fetch_service = (TextView) findViewById(R.id.fetch);
web_service = (Button) findViewById(R.id.web_service);
web_service.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myasynctask MAT = new myasynctask();
MAT.execute();
}
});
}

class myasynctask extends AsyncTask<String, Void, String> {

String str;
private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private static final String METHOD_NAME = "CelsiusToFahrenheit";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";

@Override
protected String doInBackground(String... params) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Celsius", "32");

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();
str = response.toString();

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

}
Log.d("WebRespone", str);
return str;
}

@Override
public void onPostExecute(String result) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
fetch_service.setText(result);
super.onPostExecute(result);
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}

}
}
Manifest:
add the following thins in your manifest
 <uses-permission android:name="android.permission.INTERNET"/>

To Do:

You Must add this KSOAP jar file in your Lib folder