Pages

Friday, September 27, 2013

Simple XML parsing in android using ksoap2.

The example is for doing an authentication by entering the username and password as Properties. On login success it returns an id, Which is used for further operations.


First of all download the latest ksoap2 - Android from here:
https://code.google.com/p/ksoap2-android/wiki/HowToUse?tm=2

And include it into your project.

In your MainActivity.java :


 private static String SOAP_ACTION = "http://tempuri.org/fnGetId";   
 private static String NAMESPACE = "http://tempuri.org/";  
 private static String METHOD_NAME = "fnGetId";  
 private static String URL = "http://187.168.1.100/api/Service.asmx";   

Using an AsyncTask :


 new AsyncTask<String, String, String>() {    
 protected void onPreExecute() {   
 progress = ProgressDialog.show(MainActivity.this, "","Authenticating...",true,true,  
 new DialogInterface.OnCancelListener() {  
     @Override  
       public void onCancel(DialogInterface dialog) {  
                  cancel(true);  
                            }  
                });  
      };  
     @Override  
     protected String doInBackground(String... params) {  
     // TODO Auto-generated method stub  
     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);  
     // add properties here       
     request.addProperty("userName", et_username.getText().toString());   
     request.addProperty("password", et_password.getText().toString());  
 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);  
           envelope.dotNet = true;  
           envelope.setOutputSoapObject(request);  
      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);  
      try {     
           androidHttpTransport.call(SOAP_ACTION, envelope);  
     if (envelope.bodyIn instanceof SoapObject) {  
            // SoapObject = SUCCESS  
             SoapObject vSoapObject = (SoapObject) envelope.bodyIn;  
             vCount = vSoapObject.getPropertyCount();  
             System.out.println("count: " + vCount);              
             SoapObject response = (SoapObject)envelope.getResponse();  
        Log.e("Soapobject response at main activity", response.toString());  
      if (response != null) {  
  SoapObject dataset = (SoapObject)            response.getProperty("NewDataSet");  
 //for <NewDataSet>tag  
 SoapObject table = (SoapObject) dataset.getProperty("Table");  
 // for <Table> tag under                                                                           
 SoapPrimitive agentid = (SoapPrimitive) table.getProperty("AGENT_ID");  
 // <AGENT_ID> tag                                                                     
                  aid = agentid.toString();  
                      }  
  } else if (envelope.bodyIn instanceof SoapFault) {  
  // SoapFault = FAILURE  
      SoapFault response = (SoapFault) envelope.bodyIn;  
      Log.e("response", response.toString());  
  }  
           } catch (Exception e) {  
           e.printStackTrace();  
           Log.e("Authentivcations error", "GLdkjgl");  
           }  
          return null;  
        }  
        protected void onPostExecute(String result) {  
        // Log.e(pdlist.size()+"size", "dfsadfas");  
          if(progress!=null)  
          {  
          progress.dismiss();  
          progress.cancel();  
         }  
          if(vCount==0){  
   Toast.makeText(MainActivity.this,"Authentication Failed" , Toast.LENGTH_LONG).show();  
          }  
 else{  
          Bundle bundle=new Bundle();  
          bundle.putString("agentid", aid);  
          Intent intent = new Intent(MainActivity.this,HomeActivity.class);  
          intent.putExtra("Bundle", bundle);  
          startActivity(intent);  
            MainActivity.this.finish();  
   }  
        };  
      }.execute();  

Thanks...:)