无法在Android上使用网络服务

时间:2014-02-21 21:30:31

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

我试图在Android应用程序中使用Web服务。我在.net c#

中创建的Web服务

我有这个例外“没有有效的动作参数就无法处理请求。请提供有效的肥皂。”。

我不知道这是什么问题,我该怎么做。

请某人帮助我!!

complet例外:

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

}
}

1 个答案:

答案 0 :(得分:0)

我发现了问题。 它很棒!!

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

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

代替,

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