Question

I am calling a ksoap web service from my android app, but it throws exception : Please see below is my code :

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

        if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    try {
        makingEnvelopAndReady(URL, METHOD_NAME, 10, 10,
                                "Daily", 5000, 10, "Hourly");
    } catch (Exception e) {
        // TODO: handle exception
        Log.v("Excepotuion in main method : ",
                "Excepotuion in main method : " + e);
    }
    }

makingEnvelopAndReady() method

public String makingEnvelopAndReady(String URL, String Method,
            int input_rate, int rateFrequencyId, String rateFrequencyName,
            int nonTaxableExpense, int expenseFrequencyId,
            String expenseFrequencyName) {

        String SOAPRequestXMLBody = "<?xml version=\\\"1.1\\\" encoding=\\\"utf-8\\\"?><s:Envelope xmlns:s=\\\"http://schemas.xmlsoap.org/soap/envelope/\\\"><s:Body><Calculate xmlns=\\\"http://tempuri.org/\\\"><inputRate>"
                + input_rate
                + "</inputRate><rateFrequency xmlns:a=\\\"http://schemas.datacontract.org/2004/07/FusionPeople.MobileContractor.DomainModel\\\" xmlns:i=\\\"http://www.w3.org/2001/XMLSchema-instance\\\"><a:RateFrequencyId>"
                + rateFrequencyId
                + "</a:RateFrequencyId><a:RateFrequencyName>"
                + rateFrequencyName
                + "</a:RateFrequencyName></rateFrequency><nonTaxableExpense>"
                + nonTaxableExpense
                + "</nonTaxableExpense><expenseFrequency xmlns:a=\\\"http://schemas.datacontract.org/2004/07/FusionPeople.MobileContractor.DomainModel\\\" xmlns:i=\\\"http://www.w3.org/2001/XMLSchema-instance\\\"><a:RateFrequencyId>"
                + expenseFrequencyId
                + "</a:RateFrequencyId><a:RateFrequencyName>"
                + expenseFrequencyName
                + "</a:RateFrequencyName></expenseFrequency></Calculate></s:Body></soap:Envelope>";

        callSOAPServer(URL, SOAP_ACTION, SOAPRequestXMLBody);

        return SOAPRequestXMLBody;
    }

callSOAPServer() Method :

private byte[] callSOAPServer(String url, String soapAction, String envelope) {

byte[] result = null;

HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 15000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
        timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 35000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);

/*
 * httpclient.getCredentialsProvider().setCredentials( new
 * AuthScope("os.icloud.com", 80, null, "Digest"), new
 * UsernamePasswordCredentials(username, password));
 */
HttpPost httppost = new HttpPost(url);
httppost.setHeader("soapaction", soapAction);
httppost.setHeader("Content-Type", "text/xml; charset=utf-8");

System.out.println("executing request" + httppost.getRequestLine());
// now create a soap request message as follows:
final StringBuffer soap = new StringBuffer();
// soap.append("\n");
// soap.append("");
// this is a sample data..you have create your own required data BEGIN
// soap.append(" \n");
// soap.append(" \n");
soap.append("" + envelope);
// soap.append(" \n");
// soap.append(" \n");

/* soap.append(body); */
// END of MEssage Body
soap.append("");
Log.i("SOAP Request", "" + soap.toString());
// END of full SOAP request message
try {
    HttpEntity entity = new StringEntity(soap.toString(), HTTP.UTF_8);
    httppost.setEntity(entity);
    HttpResponse response = httpclient.execute(httppost);// calling
                                                            // server
    HttpEntity r_entity = response.getEntity(); // get response
    Log.i("Reponse Header", "Begin..."); // response headers
    Log.i("Reponse Header", "StatusLine:" + response.getStatusLine());
    Header[] headers = response.getAllHeaders();
    for (Header h : headers) {
        Log.i("Reponse Header", h.getName() + ": " + h.getValue());
    }
    Log.i("Reponse Header", "END...");
    if (r_entity != null) {
        result = new byte[(int) r_entity.getContentLength()];
        if (r_entity.isStreaming()) {
            DataInputStream is = new DataInputStream(
                    r_entity.getContent());
            is.readFully(result);
        }
    }
} catch (Exception E) {
    Log.i("Exception While Connecting", "" + E.getMessage());
    E.printStackTrace();
}

httpclient.getConnectionManager().shutdown(); // shut down the
                                                // connection
return result;
}

It Throwing below Error :

09-11 19:48:39.672: I/System.out(2459): executing requestPOST http://brainteclabs.com:8180/MobileContractorCalculationService.svc HTTP/1.1
09-11 19:48:39.672: I/SOAP Request(2459): <?xml version=\"1.1\" encoding=\"utf-8\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><Calculate xmlns=\"http://tempuri.org/\"><inputRate>10</inputRate><rateFrequency xmlns:a=\"http://schemas.datacontract.org/2004/07/FusionPeople.MobileContractor.DomainModel\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><a:RateFrequencyId>10</a:RateFrequencyId><a:RateFrequencyName>Daily</a:RateFrequencyName></rateFrequency><nonTaxableExpense>5000</nonTaxableExpense><expenseFrequency xmlns:a=\"http://schemas.datacontract.org/2004/07/FusionPeople.MobileContractor.DomainModel\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><a:RateFrequencyId>10</a:RateFrequencyId><a:RateFrequencyName>Hourly</a:RateFrequencyName></expenseFrequency></Calculate></s:Body></soap:Envelope>
09-11 19:48:49.853: I/Reponse Header(2459): Begin...
09-11 19:48:49.853: I/Reponse Header(2459): StatusLine:HTTP/1.1 400 Bad Request
09-11 19:48:49.863: I/Reponse Header(2459): Cache-Control: private
09-11 19:48:49.863: I/Reponse Header(2459): Server: Microsoft-IIS/8.0
09-11 19:48:49.863: I/Reponse Header(2459): Set-Cookie: ASP.NET_SessionId=ebjsi0xuqhbprtzo1dozjkws; path=/; HttpOnly
09-11 19:48:49.863: I/Reponse Header(2459): X-AspNet-Version: 4.0.30319
09-11 19:48:49.863: I/Reponse Header(2459): X-Powered-By: ASP.NET
09-11 19:48:49.863: I/Reponse Header(2459): Date: Wed, 11 Sep 2013 14:19:07 GMT
09-11 19:48:49.873: I/Reponse Header(2459): Content-Length: 0
09-11 19:48:49.887: I/Reponse Header(2459): END...

This is data information :

   private final String URL = "http://brainteclabs.com:8180/MobileContractorCalculationService.svc";
    private final String SOAP_ACTION = "http://tempuri.org/IMobileContractorCalculationService/Calculate";
    private final String METHOD_NAME = "Calculate";

I have struggled so much and used so many ways to request ksoap web service but still no getting any successful result, Please see my code and tell me where i am missing something.

Was it helpful?

Solution

Remove extra characters from you envelop like "//". Now it should be looks like below

String SOAPRequestXMLBody = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><Calculate xmlns=\"http://tempuri.org/\"><inputRate>"
                + "10"
                + "</inputRate><rateFrequency xmlns:a=\"http://schemas.datacontract.org/2004/07/FusionPeople.MobileContractor.DomainModel\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><a:RateFrequencyId>"
                + "10"
                + "</a:RateFrequencyId><a:RateFrequencyName>"
                + "Hourly"
                + "</a:RateFrequencyName></rateFrequency><nonTaxableExpense>"
                + "5000"
                + "</nonTaxableExpense><expenseFrequency xmlns:a=\"http://schemas.datacontract.org/2004/07/FusionPeople.MobileContractor.DomainModel\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><a:RateFrequencyId>"
                + "10"
                + "</a:RateFrequencyId><a:RateFrequencyName>"
                + "Hourly"
                + "</a:RateFrequencyName></expenseFrequency></Calculate></s:Body></s:Envelope>";`enter code here`

Now it will working fine.....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top