【问题标题】:Getting Exception While Invoking Web Service From Android从 Android 调用 Web 服务时出现异常
【发布时间】:2017-09-01 08:08:56
【问题描述】:

我是这方面的初学者,我在从 android 调用 asp.net Web 服务方法 (SignUp) 时遇到问题。当我从浏览器尝试它时它工作正常,但是当我从 android 做它时它给了我一些例外,比如 "

SoapFault - 故障代码:'soap:Client' 故障字符串:'System.Web.Services.Protocols.SoapException:服务器无法识别 HTTP 标头 SOAPAction 的值:SignUp。 在 System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest() 在 System.Web.Services.Protocols.SoapServerProtocol.Initialize() 在 System.Web.Services.Protocols.ServerProtocol.SetContext(类型类型,HttpContext 上下文,HttpRequest 请求,HttpResponse 响应) 在 System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)' faultactor: 'null' detail: org.kxml2.kdom。"

这是我的 Java 注册类

String SOAP_ACTION1 = "http://tempuri.org/SignUp";
String METHOD_NAME1 = "SignUp";
btn = (Button)findViewById(R.id.btn_signup);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new WebService().execute(METHOD_NAME1,SOAP_ACTION1,txt_name.getText().toString(),                  txt_email.getText().toString(),txt_password.getText().toString(),
                    txt_address.getText().toString(),
                    txt_dob.getText().toString(),
                    txt_account.getText().toString(),                       
                 txt_cnic.getText().toString(),txt_pobox.getText().toString(),                       
               txt_city.getText().toString(),txt_status.getText().toString());

这是我的 Java WebService 类

 private static String NAMESPACE = "http://tempuri.org/";
private static String URL = "http://192.168.0.102/MyWebService/WebService1.asmx?WSDL";
 protected String doInBackground(String... params) {
    String s="";
    String whichmethodcall = params[0];
    switch(whichmethodcall){
        case "SignUp":
            s = signup(params);
            break;
        case "SignIn":
            s = signin(params);
            break;
    }
    return s;
}
 public String signup(String[] arr){
    String s = "";
    SoapObject request = new SoapObject(NAMESPACE, arr[1]);
    //Use this to add parameters
    request.addProperty("cname",arr[2]);
    request.addProperty("cemail",arr[3]);
    request.addProperty("cpwd",arr[4]);
    request.addProperty("caddress",arr[5]);
    request.addProperty("cdob",arr[6]);
    request.addProperty("caccount",arr[7]);
    request.addProperty("cCnic",arr[8]);
    request.addProperty("cpobox",arr[9]);
    request.addProperty("cCity",arr[10]);
    request.addProperty("cstatus",arr[11]);

    //Declare the version of the SOAP request
    SoapSerializationEnvelope envelope = new 
    SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);
    envelope.dotNet = true;

    try {
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        //this is the actual part that will call the webservice
        androidHttpTransport.call(arr[0], envelope);

        // Get the SoapResult from the envelope body.
       // SoapObject result = (SoapObject)envelope.bodyIn;
       // SoapObject result = (SoapObject) envelope.getResponse();
        if (envelope.bodyIn instanceof SoapFault) {
            final SoapFault result = (SoapFault) envelope.bodyIn;
            if (result != null) {
                //Get the first property and change the label text
                   s = result.toString();
            } else
                s = "No response";              
        }
    } catch (Exception e) {
        e.printStackTrace();
        s=e.getMessage().toString();
    }
    return s;
}

这是我的 asp.net WebService 方法

 public string SignUp(string cname, string caddress, string cdob, long caccount, long cCnic, int cpobox, string cCity, string cstatus,string cemail, string cpass)
    {
        bool check = ConnectDB();           // Opens the Connection to Insert The new User
        string msg = "";
        //DateTime dob = DateTime.ParseExact(cdob, "dd/mm/yyyy", null);
        string query = "INSERT INTO Customers values('" + cname + "','" + caddress + "','" + cdob + "','" + caccount + "','" + cCnic + "','" + cpobox + "','" + cCity + "','" + cstatus + "','"+ cemail + "','"+ cpass + "')";
        SqlCommand comm = new SqlCommand(query, con);
        try{
            if (check == true){
                if (comm.ExecuteNonQuery() > 0)
                    msg = "you've signed up !";
                else
                    msg = "sign up error";                   
            }
            else               
                msg = "Database not Connected";               
        }catch (SqlException ex){
            msg = ex.Message.ToString();
            con.Close();
        }finally{
            con.Close();
        }
        return msg;
    }

【问题讨论】:

  • 看起来您正在使用 ksoap2。 Transport 有两个字段 requestDump 和 responseDump 对调试很有帮助。你能在 finally 子句中添加这样的东西吗
  • if (transport.requestDump != null) { Log.d(getClass().getName(), methodId + " " + transport.requestDump); } else { Log.d(getClass().getName(), "没有请求转储" + methodId); } if (transport.responseDump != null) { Log.d(getClass().getName(), methodId + " " + transport.responseDump); } else { Log.d(getClass().getName(), "没有响应转储" + methodId); }

标签: java android asp.net web-services


【解决方案1】:

你可以试试这个

而不是 SoapObject 请求 = new SoapObject(NAMESPACE, arr[1]); 使用

SoapObject request = new SoapObject(NAMESPACE, arr[0]);

在soap对象中你应该只传递方法名,没有http://...

【讨论】:

  • arr[2] 包含客户名称。我应该在 this 中传递方法名称。没有?
  • 是的,刚刚看到.. 没有任何变化,我仍然遇到同样的异常
  • 在原始消息下查看我的评论。尝试打印 requestDump 和 responseDump
  • 忘了说你必须在创建传输后添加 androidHttpTransport.debug = true
  • 另一个问题我看到参数不匹配您发送“cpwd”,但服务器需要“cpass”
猜你喜欢
  • 1970-01-01
  • 2020-10-27
  • 1970-01-01
  • 2010-12-11
  • 2018-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多