Web Service不适用于Android应用程序

时间:2016-03-31 19:04:01

标签: java android web-services soap

我想将数据发送到WebService,但它没有返回有效的反馈,并在String上显示错误。我正在使用KSOAP2。

<小时/> 当我调试时,我收到以下消息:

  

已收集对象无法评估org.ksoap2.serialization.SoapObject.toString()

如果一切正常,它应该回答我'OK',但WebService返回一个XML(responseDump):

  

&LT; ?xml version =“1.0”encoding =“utf-8”?&gt;&lt; soap:Envelopexmlns:soap =“http://schemas.xmlsoap.org/soap/envelope/”xmlns:xsi =“http://www.w3.org/2001/XMLSchema-instance”xmlns:xsd =“http: //www.w3.org/2001/XMLSchema"><皂:Faultsoap:服务器16; /&的faultcode LT; faultstring&gt; System.Web.Services.Protocols.SoapException:服务器无法处理请求。   ---&gt; System.InvalidOperationException: CommandText属性尚未初始化。   在System.Data.OracleClient.OracleClient.OracleClient.OracleClient.OracleClient.OracleClmand.OracleClient.OracleClmand.OracleClient.Oracle上的System.Data.OracleClient.OracleClmand.get_StatementText()处于System.Data.OracleClient.OracleCommand.Execute(OciStatementHandle statementHandle,CommandBehavior behavior,Boolean needRowid,OciRowidDescriptor&amp; rowidDescriptor,ArrayList&amp; resultParameterOrdinals) (布尔值needRowid,OciRowidDescriptor&amp; rowidDescriptor)位于MettaWebService.BancoDados.executaComando(String查询)的System.Data.OracleClient.OracleCommand.ExecuteNonQuery(),位于mobPrev_SAP.wsvmetta.EnviarDados(字符串查询)的MettaWebService.MettaServicos.EnviarDados(字符串查询)在c:\ Users \ Deivite \ AppData \ Local \ Temp \ Compilacao \ wsvMetta_Eng \ wsvmetta.asmx.cs:第65行--- 内部异常堆栈跟踪结束 ---

以下是发送和接收WebService的代码:

public String transmitWS(String col_numero, String qtd_consumo, String idate_itime, String vlr_med,
                         String cod_valor, String matric, String texto_obs, String observacao,
                         String idProgramacao){
    String HOST = "";
    String NAMESPACE = "";
    String URL = "";              //all right here
    String SOAP_ACTION = "";
    String METHOD_NAME = "";

    String resultWS = null;

    SoapObject requestTransmit = new SoapObject(NAMESPACE, METHOD_NAME);
    PropertyInfo transmitPI = new PropertyInfo();
    transmitPI.setType(String.class);
    transmitPI.setName("Query");
    transmitPI.setValue("update prev_programacao set col_numero = " + col_numero + ", status_ponto = 3, qtd_consumo =" + qtd_consumo + "," +
            " idate_itime = TO_DATE('"+ idate_itime +"','YYYY-MM-DD HH24:MI:SS'), vlr_med = " + vlr_med + ", cod_valor = " + cod_valor +
            ", matric = " + matric +", texto_obs = '" + texto_obs + "', observacao = '" +
            observacao + "' where id_programacao = "+idProgramacao+"");
    requestTransmit.addProperty(transmitPI);
    SoapSerializationEnvelope envelopeTransmit = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelopeTransmit.dotNet = true;
    envelopeTransmit.setAddAdornments(false);
    envelopeTransmit.implicitTypes = false;
    envelopeTransmit.setOutputSoapObject(requestTransmit);
    HttpTransportSE androidHttpTransportTransmit = new HttpTransportSE(URL);
    //AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
    try {
        androidHttpTransportTransmit.debug = true;
        androidHttpTransportTransmit.call(SOAP_ACTION, envelopeTransmit);
        SoapObject responseTransmit = (SoapObject) envelopeTransmit.getResponse();
        resultWS = androidHttpTransportTransmit.requestDump;
        resultWS = androidHttpTransportTransmit.responseDump;

        Log.i("t", "doInBackground");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return resultWS;
}

最后,resultWS为null。 是否有人知道此错误的原因?

1 个答案:

答案 0 :(得分:0)

我无法检测到什么是错的,但这里是一个使用kso​​ap的工作函数。也许它会有所帮助:

    /**
 * Calls the stored proc on the server to return a dataset/SoapObject. This will always run the ProcReader method on the web server.
 * 
 * @param storedProc The Stored Proc to call
 * @param params The parameter(s) for the stored proc (pipe seperated for multiple parameters)
 * @return The SoapObject returned by the Stored Proc call
 * @throws Exception
 */
public static SoapObject callProcServiceForObject(String serviceMethod, String storedProc, String params) throws Exception {
    String NAMESPACE = "http://" + GlobalVars.serviceIP + "/";
    String METHOD_NAME = getMethodName(serviceMethod);
    String SOAP_ACTION = NAMESPACE + METHOD_NAME;
    String URL = "http://" + GlobalVars.serviceIP + "/ATService.asmx";
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    if (GlobalVars.encryptedService) {
        request.addProperty("ePassword", CryptUtil.encryptString(GlobalVars.deviceSerialNumber));
        request.addProperty("eData", CryptUtil.encryptString(GlobalVars.serverDB));
        request.addProperty("eSP_Name", CryptUtil.encryptString(storedProc));
        request.addProperty("eParam", CryptUtil.encryptString(params));
    } else {
        request.addProperty("sPassword", GlobalVars.deviceSerialNumber);
        request.addProperty("sData", GlobalVars.serverDB);
        request.addProperty("sSP_Name", storedProc);
        request.addProperty("sParam", params);
    }
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

    // Enable the below property if consuming .Net service
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, timeout);
    //count up the network traffic
    numberOfBytesTransmitted = numberOfBytesTransmitted + StringToBytes(request.toString());

    SoapObject returnable = null;
    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        returnable = (SoapObject)envelope.getResponse();
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Msg:" + e.getMessage() + "; SP:" + storedProc + "; Params: " + params + "; Method:" + METHOD_NAME);
    }
    numberOfBytesTransmitted = numberOfBytesTransmitted + StringToBytes(returnable.toString());
    return returnable;
}
相关问题