Thursday, 4 August 2016

SOAP | XMl Parsing Example

Step1:- Open your Android Studio.
Step2:-Click on File >New >New Project> SOAPExample
Step3:-Click on res>activity_main.xml and take 1TextView and 1 Spinner
Step 4:- activity_main.xml code will be like following.

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="supriya.soap2.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select Country"
        android:id="@+id/textView" />

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="58dp" />
</RelativeLayout>

Step 5:- Click to Download ksoap2 dependencies library  After downloading extract the file and copy that library file and paste that in libs folder. app >libs >after paste right click on that ksoap2 file and select > set as library > ok
Step 6:- MainActivity.java code will be like following.

package supriya.soap2;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;

public class    MainActivity extends AppCompatActivity {
    Spinner spnCountry;
    ProgressDialog pd;

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

        //process dialoge coding...        
pd=new ProgressDialog(MainActivity.this);
        pd.setMessage("Loading..");

        //id of Spinner..        
spnCountry=(Spinner)findViewById(R.id.spinner);

        //array list...        
ArrayList<String> list=new ArrayList<String>();
        list.clear();
        list.add("--Select--");
        list.add("India");
        list.add("US");


        //array adapter...        
ArrayAdapter<String> adp=new ArrayAdapter<String>(getApplicationContext(),R.layout.spinback,list);
        spnCountry.setAdapter(adp);

        //spinner listner,.....        
spnCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override            
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                MyTask t=new MyTask(spnCountry.getSelectedItem().toString());
                t.execute();

            }

            @Override            
public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }//closing of onCreate method......
    class MyTask extends AsyncTask<String,String,String>{
        //variables....        
String setCountryName="";
        String getCitiesName="";

        //constructor.....        
public  MyTask(String cName){
            this.setCountryName=cName;

        }
        @Override        
protected void onPreExecute() {
            super.onPreExecute();
            pd.show();
        }

        @Override        
protected String doInBackground(String... params) {
            AddEvent ae=new AddEvent();
            getCitiesName=ae.getResultOfCitiesName(setCountryName);
            return null;
        }

        @Override        
protected void onPostExecute(String s) {
            super.onPostExecute(s);
            tostOutput(getCitiesName);
            pd.dismiss();
        }

        @Override        
protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
        }
    }
//close MyTask class
    public void tostOutput(String result){
        Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
    }
}
Step 6:- AddEvent.java code will be like following.

public class AddEvent {
    String NAMESPACE = "http://www.webserviceX.NET";
    String URL="http://www.webservicex.net/globalweather.asmx?wsdl";


    public String getResultOfCitiesName(String getAllCitiesName)
    {
        String SOAP_ACTION1 = "http://www.webserviceX.NET/GetCitiesByCountry";
        String METHOD_NAME1 = "GetCitiesByCountry";
        String result_="";
        SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME1);

        request.addProperty("CountryName",getAllCitiesName);

        SoapSerializationEnvelope envelope =new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
        envelope.dotNet = true;
        try        {
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            //this is the actual part that will call the webservice         
androidHttpTransport.call(SOAP_ACTION1, envelope);

            // Get the SoapResult from the envelope body.            
SoapObject result = (SoapObject)envelope.bodyIn;
            if(result != null)
            {
                result_=result.getProperty(0).toString().trim();
            }
            else            {
                result_="no data found";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result_;
    }
}

Step 6:-To give customlook to spineer add a xml file in layout folder  app > res > layout > right click on layout > new > layout resource file > spinback.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:textColor="#000000"
    android:minHeight="?android:attr/listPreferredItemHeightSmall" />




No comments:

Post a Comment