【问题标题】:unable to consume a web service on Android无法在 Android 上使用 Web 服务
【发布时间】:2014-02-21 21:30:31
【问题描述】:

我正在尝试在 Android 应用程序中使用 Web 服务。我在 .net c# 中创建的 Web 服务

我有这个异常“无法处理没有有效操作参数的请求。请提供有效的肥皂。”。

我不知道应该是什么问题,我该怎么办。

请有人帮助我!!

完全异常:

     Code: soap:Sender, Reason: System.Web.Services.Protocols.SoapException: unable to handle request without a valid action parameter. Please supply a valid soap.
à System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()
à System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
à System.Web.Services.Protocols.SoapServerProtocol.Initialize()
à System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
à System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean
at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:143)
at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:140)
at org.ksoap2.transport.Transport.parseResponse(Transport.java:118)
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:272)
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)

这是我的代码,用于使用 Web 服务:

public class WebserviceCall {

 /**
     * Variable Decleration................
    * 
     */
    String namespace = "http://10.0.2.2:1378/";
    private String url = "http://10.0.2.2:1378/Service1.asmx";

    String SOAP_ACTION;
    SoapObject request = null, objMessages = null;
    SoapSerializationEnvelope envelope;
    HttpTransportSE androidHttpTransport;

public WebserviceCall() {
    // TODO Auto-generated constructor stub
}

/**
     * Set Envelope
     */
protected void SetEnvelope() {

    try {

        // Creating SOAP envelope           
        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);

        //You can comment that line if your web service is not .NET one.
        envelope.dotNet = true;

        envelope.setOutputSoapObject(request);
        androidHttpTransport = new HttpTransportSE(url);
        androidHttpTransport.debug = true;

    } catch (Exception e) {
        System.out.println("Soap Exception---->>>" + e.toString());    
    }
  }

   //Authentification consuming WS
  public client GetClientByAuthentification(String email,String password)
  {

        SOAP_ACTION = namespace + "GetClientByAuthentification";

        //Adding values to request object
        request = new SoapObject(namespace, "GetClientByAuthentification");


        //Adding String value to request object
        request.addProperty("eMail", "" + email);
        request.addProperty("Password", "" + password);

        SetEnvelope();



        //SOAP calling webservice
          try {
        androidHttpTransport.call(SOAP_ACTION, envelope);


        //Got Webservice response
        String result;

        //result = envelope.getResponse().toString();

        SoapObject resp=(SoapObject) envelope.getResponse();
        result=resp.toString();
        Log.e("","result: "+result);

        client clt= new client();
        clt.setClientFromXmlString(result);
        if(!(clt.getNom().isEmpty()))
                    return clt;
        else 
            return null;

        } catch (SoapFault e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         catch (HttpResponseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
     }

与 Web Service 应用程序相关的代码:

namespace WebService1
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
    string connectionString = "Data Source=TASSINEMOUHCINE;Initial Catalog=test;Integrated Security=True;Pooling=False";
    [WebMethod]
    public string GetClientByAuthentification(string eMail, string Password)
    {
        SqlConnection connexion = new SqlConnection(connectionString);

        SqlCommand command = new SqlCommand();
        //add Parameters
        command.Parameters.Add("@eMail", SqlDbType.VarChar).Value = eMail;
        command.Parameters.Add("@Password", SqlDbType.VarChar).Value = Password;

        //Execution de la requête 2 permattant de calculer et stocker l'Age
        command.Connection = connexion;
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "GetClientByAuthentification";
        //
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(command);
        da.Fill(ds);


        return contextTable(ds, 0, "client");
    }

      private string contextTable(DataSet ds, int indiceTab, string balisePrincipalName)
    {
        string context = "";
        int i = 0;
        foreach (DataRow row in ds.Tables[indiceTab].Rows)
        {
            context += "<" + balisePrincipalName + " id=\""+(i++)+"\">";
            foreach (DataColumn col in ds.Tables[indiceTab].Columns)
            { 

                context += "<" + col.ColumnName + ">";
                context += row[col.ColumnName].ToString();
                context += "<" + col.ColumnName + "/>";

            }
            context += "<" + balisePrincipalName + "/>";

        }
          return context;
        }

}
}

【问题讨论】:

    标签: c# android .net web-services android-ksoap2


    【解决方案1】:

    我发现了问题。 效果很好!!

    唯一的错误是java代码中命名空间的值,应该是

       String namespace="http://tempuri.org/";
    

    相反,

       String namespace = "http://10.0.2.2:1378/";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      相关资源
      最近更新 更多