WebService FAQ


Q:-How to parse WebService XML For Android?

   Here is the Step By Step Description of Parsing Of Returned XML By WebService.




//SET THESE VARIABLE AS DEFINE BELOW

      private String METHOD_NAME ="IshratMethod";
      private String NAMESPACE = "http://tempuri.org/";
private String SOAP_ACTION ="http://tempuri.org/IshratMethod";
private String URL = "http://ishratkhan007/Service1.asmx?WSDL";

//CREATE FUNCTION THAT RETURN HASHMAP TYPE ARRAYLIST AS DEFINE BELOW
public ArrayList<HashMap<String,String>> GetWebMethod(String ItemId) {
    SoapObject Resulr = null;
    ArrayList<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>();
String value="";
              try {
// Initialize soap request + add parameters
         SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("ItemId", ItemId);
// Declare the version of the SOAP request
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
// Needed to make the internet call
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
  androidHttpTransport.debug = true;
// this is the actual part that will call the webservice
                     androidHttpTransport.call(SOAP_ACTION, envelope);
                     // Get the SoapResult from the envelope body.
         Resulr= (SoapObject) envelope.getResponse();
        if (Resulr != null) {
   // Get the first property and change the label text                       
  SoapObject response1=(SoapObject) Resulr.getProperty(1); 
SoapObject response2 = (SoapObject) response1.getProperty(0);
if(response2.getPropertyCount()>0)
{
for (int i = 0; i <response2.getPropertyCount(); i++) {
Object property = response2.getProperty(i);
if (property instanceof SoapObject) {
SoapObject countryObj = (SoapObject) property;
HashMap<String,String> map=new HashMap<String,String>();                
map.put("ML_LOCATION",countryObj.getPropertySafelyAsString("ML_LOCATION", "Nill").toString());
 
map.put("ML_STATUS",countryObj.getPropertySafelyAsString("ML_STATUS", "Nill").toString());
                  map.put("ML_SECONDARY",countryObj.getPropertySafelyAsString("ML_SECONDARY", "Nill").toString());
                   map.put("ML_QTY",countryObj.getPropertySafelyAsString("ML_QTY", "Nill").toString());
                 list.add(map);
}
                           }
                           }
                     }

              } catch (Exception e) {
         System.out.println("exception ="+ e.toString());
              }
              return list;

       }


_________________________________________________________________________________

Q:- Not able to access the WebService From Android Device but working in Emulator.

     ,First WebService Called But Exception in Second Call fails second time ? ,


Ans For All Above Questions:-

  The connection pool used by HttpURLConnection when it is keeping connections alive is broken such that it tries to use connections that have been closed by the server. By default Android sets KeepAlive on all connections.
System.setProperty("http.keepAlive", "false"); is a workaround that disables KeepAlive for all connections so then you avoid the bug in the connection pool.
conn.setRequestProperty("Connection","Keep-Alive"); turns KeepAlive on for this particular connection, essentially reversing what System.setProperty("http.keepAlive", "false"); does.
Also I always explicitly call connect() as it makes it clear where you are ending your connection setup. I'm not sure if calling this method is optional or not. (By Barrycburton)
System.setProperty("http.keepAlive", "false");

No comments:

Post a Comment