Tuesday, September 22, 2009

ksoap-Accessing web service with BlackBerry

Check weather given username and password are correct by connecting to remote server.returns true or false according to server response

Add ksoap jar module to your project

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;
import org.xmlpull.v1.XmlPullParserException;

public boolean createSoap(String username,String password)
{
    boolean ans=false;
    SoapObject request = new SoapObject("http://tempuri.org/","isValidUser");
     request.addProperty("Username",username);
     request.addProperty("Password",password);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.bodyOut = request;
    envelope.dotNet = true;
    HttpTransport ht = new HttpTransport("http://164.49.254.143:8080/Service.asmx");
    ht.debug = true;
    try {
        ht.call("http://tempuri.org/isValidUser", envelope);     
        if((envelope.getResponse().toString()).equals("true"))
        {
            return true;   
        }
    }
    catch (XmlPullParserException ex)
    {
        Dialog.alert("ex1 "+ex.toString() );
        ex.printStackTrace();
    } catch (IOException ex) {
        Dialog.alert("ex2 " +ex.toString());
        ex.printStackTrace();
    } catch (Exception ex) {
        Dialog.alert("ex3 " + ex.toString());
    }
    return false;
}

No comments:

Post a Comment