从android获取soap web服务的Xml String请求

时间:2013-09-11 14:21:34

标签: android android-ksoap2

我从我的Android应用程序调用ksoap Web服务,但它抛出异常: 请参阅下面的代码:

@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);
    }
    }

makeEnvelopAndReady()方法

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()方法:

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;
}

抛出错误:

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...

这是数据信息:

   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";

我一直在努力工作,并且使用了很多方法来请求ksoap网络服务,但仍然没有取得任何成功的结果,请查看我的代码并告诉我我在哪里遗漏了什么。

1 个答案:

答案 0 :(得分:0)

从中删除额外的字符,如“//”。现在它应该看起来像下面

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`

现在它会正常工作.....