0

我正在从我的 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);
    }
    }

制作EnvelopAndReady() 方法

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 Web 服务,但仍然没有得到任何成功的结果,请查看我的代码并告诉我我在哪里遗漏了一些东西。

4

1 回答 1

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`

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

于 2013-09-12T09:58:18.560 回答