Monday, August 20, 2012

Soap Example in Android

web service android example

package com.soft.info.getcustomer;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.soft.info.util.SoapParse;

public class GetCustomerActivity extends Activity {
               
                TextView tv;
                ListView lv;
                private ArrayAdapter<String> listAdapter ; 
private static String SOAP_ACTION = "http://tempuri.org/GetCustomer";
               
                private static String NAMESPACE = "http://tempuri.org/";
                private static String METHOD_NAME = "GetCustomer";
               
private static String URL = "http://demo.net/soapservice/test.asmx";
//this url doesn’t exist just like example  of .net soap web service
               
               
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_customer);
        tv=(TextView)findViewById(R.id.textvalue);
        lv=(ListView)findViewById(R.id.listView1);
        //Initialize soap request + add parameters
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);       
        request.addProperty("cGroupCode","00");
        request.addProperty("cID","1212121212");
        request.addProperty("cMode","1");
            
       
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
    
        // Make the soap call.
                                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        try {
               
                //this is the actual part that will call the webservice
                                                androidHttpTransport.call(SOAP_ACTION, envelope);       
        } catch (Exception e) {
                e.printStackTrace();
        }
       
                                // Get the SoapResult from the envelope body.                               
                                SoapObject result = (SoapObject)envelope.bodyIn;
                                                               
                                if(result != null){
                               
                                                try{
                                                                 
                                                                String str=envelope.getResponse().toString();
                                                                //System.out.println("str="+str);
                                                                SoapParse sr=new SoapParse();
                 listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, sr.getCustomer(str)); 

                lv.setAdapter(listAdapter);
                }
            catch(Exception e){
                System.out.println("error"+e.getLocalizedMessage());
            }
                                }
                                else{
                                                System.out.println("url not found");

                                }
   
    }
   

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_get_customer, menu);
        return true;
    }
}

5 comments:

  1. Good but it can be better if you add some more comment for understanding Android noobs.

    ReplyDelete
  2. Hello Himanshu.i do not understand SoapParse sc=new SoapParse(); actually this line show the error. please help me

    ReplyDelete