Saturday, February 4, 2017

How to call webservice in Android

Just insert code below in your project and as per requirement add Key/Value pairs in request.addProperty. Whereas StrictMode is a developer tool which detects things you might be doing by accident and brings them to your attention so you can fix them.

StrictMode is most commonly used to catch accidental disk or network access on the application’s main thread, where UI operations are received and animations take place. Keeping disk and network operations off the main thread makes for much smoother, more responsive applications. By keeping your application’s main thread responsive, you also prevent ANR dialogs from being shown to users.
 public String callGetSecurityQuestions() throws Exception{  
     String NAMESPACE = "YOUR NAMESPACE HERE" //e.g. "http://namespace/";  
     String URL = "YOUR URL HERE" //e.g. "http://domainname/asmx name";  
     String METHOD_NAME = "YOUR METHOD NAME" //e.g. "GetSecurityQuestions";  
     String SOAP_ACTION = NAMESPACE+METHOD_NAME;  
     String response = "";  
     try {  
       if (android.os.Build.VERSION.SDK_INT > 9) {  
         StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();  
         StrictMode.setThreadPolicy(policy);  
       }  
       SoapObject request = new SoapObject(CommonEnviornment.NAMESPACE,METHOD_NAME);  
       request.addProperty("key1", value1);        
       request.addProperty("key2", value2);  
       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);  
       envelope.dotNet = true;  
       envelope.setOutputSoapObject(request);  
       HttpTransportSE androidHttpTransport = new HttpTransportSE(CommonEnviornment.URL);  
       androidHttpTransport.call(SOAP_ACTION, envelope);  
       SoapObject result = (SoapObject)envelope.bodyIn;  
       response = result.toString();  
       System.out.println(result.toString());  
     } catch (Exception e) {  
       throw e;  
     }  
     return response;  
   }  

No comments :

Post a Comment